[go: up one dir, main page]

0% found this document useful (0 votes)
2 views3 pages

Python_File_IO_Commands

This document provides a comprehensive guide on Python file input/output (I/O) commands, including how to open, write, read, append, and delete files. It emphasizes best practices, such as using 'with open' for file handling, and includes examples for each command. Additionally, it covers checking for file existence and creating directories and files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Python_File_IO_Commands

This document provides a comprehensive guide on Python file input/output (I/O) commands, including how to open, write, read, append, and delete files. It emphasizes best practices, such as using 'with open' for file handling, and includes examples for each command. Additionally, it covers checking for file existence and creating directories and files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python File Input/Output (I/O) - All Commands

PYTHON FILE INPUT/OUTPUT (I/O) - ALL COMMANDS

1. OPENING A FILE

Syntax:
file = open("filename.txt", "mode")

Modes:
'r' : Read (default)
'w' : Write (creates file or overwrites)
'a' : Append
'x' : Create (fails if file exists)
'b' : Binary mode
't' : Text mode (default)
'r+' : Read and Write

Example:
file = open("data.txt", "w")

2. WRITING TO A FILE

Syntax:
file.write("your text")

Example:
file = open("data.txt", "w")
file.write("Welcome to Python!")
file.close()

3. READING FROM A FILE

Methods:
file.read(size) Read 'size' characters
file.readline() Read one line
file.readlines() Read all lines (as list)

Example:
file = open("data.txt", "r")
content = file.read()
print(content)
file.close()

4. APPENDING TO A FILE

Example:
Python File Input/Output (I/O) - All Commands

file = open("data.txt", "a")


file.write("\nThis is an appended line.")
file.close()

5. USING "WITH OPEN" (BEST PRACTICE)

Syntax:
with open("filename", "mode") as file:
# do something

Example:
with open("data.txt", "r") as file:
content = file.read()
print(content)

6. READING FILE LINE BY LINE

Example:
with open("data.txt", "r") as file:
for line in file:
print(line.strip())

7. FILE OBJECT METHODS

file.read(size)
file.readline()
file.readlines()
file.write(text)
file.writelines(list_of_strings)
file.seek(offset)
file.tell()
file.close()

8. CHECK IF FILE EXISTS

import os
if os.path.exists("data.txt"):
print("File exists")
else:
print("File not found")

9. DELETE A FILE

import os
os.remove("data.txt")
Python File Input/Output (I/O) - All Commands

10. CREATE DIRECTORY & FILE

import os
os.mkdir("myfolder") # create folder
open("myfolder/new.txt", "w") # create file inside folder

SUMMARY (QUICK COMMANDS)

Open File open("file.txt", "r")


Write File file.write("text")
Read File file.read() / file.readline()
Append File open("file.txt", "a")
Close File file.close()
With Block with open(...) as file:
Delete File os.remove("file.txt")
Check Exists os.path.exists("file.txt")

You might also like