File handling in Python is essential for reading and writing data to files. Python provides built-in functions to handle file operations such as opening, reading, writing, and closing files.
- The key function for working with files in Python is the open() function.
- The open() function takes two parameters; filename, and mode.
- There are six different methods (modes) for opening a file:
file = open("filename", "mode") "filename": Name of the file to be opened."mode": Mode in which the file should be opened.
| Mode | Description |
|---|---|
'r' |
Read mode (default). Opens a file for reading. |
'w' |
Write mode. Creates a new file or overwrites an existing file. |
'a' |
Append mode. Adds data to the end of the file. |
'x' |
Exclusive creation. Fails if the file exists. |
'b' |
Binary mode (used with 'rb', 'wb', etc.). |
't' |
Text mode (default, used with 'rt', 'wt', etc.). |
To read a file, we use the read() method or readline() / readlines().
file = open("example.txt", "r") # Open file in read mode
content = file.read() # Read entire file content
print(content)
file.close() # Close the filefile = open("example.txt", "r")
for line in file:
print(line.strip()) # Removes newline character
file.close()file = open("example.txt", "r")
print(file.readline()) # Reads one line
print(file.readlines()) # Reads all lines and returns a list
file.close()To write data into a file, we use write() or writelines().
file = open("example.txt", "w") # Open file in write mode
file.write("Hello, World!\n") # Write a single line
file.write("Python file handling example.")
file.close()file = open("example.txt", "a") # Open file in append mode
file.write("\nAppending new text.")
file.close()Using with open() automatically closes the file after use, preventing file corruption.
with open("example.txt", "r") as file:
content = file.read()
print(content) # File automatically closes after exiting the blockFor handling non-text files (images, audio, etc.), use binary mode.
with open("image.jpg", "rb") as source:
with open("copy.jpg", "wb") as destination:
destination.write(source.read())To handle errors (e.g., file not found), use try-except.
try:
with open("missing_file.txt", "r") as file:
content = file.read()
print(content)
except FileNotFoundError:
print("File not found. Please check the file name.")Use the os and pathlib modules for handling file paths.
import os
if os.path.exists("example.txt"):
print("File exists")
else:
print("File not found")from pathlib import Path
file_path = Path("example.txt")
if file_path.exists():
print("File exists")Python’s csv module allows reading and writing CSV files.
import csv
with open("data.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Name", "Age", "City"])
writer.writerow(["Alice", 25, "New York"])
writer.writerow(["Bob", 30, "San Francisco"])with open("data.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)Python’s json module helps in working with JSON data.
import json
data = {"name": "Alice", "age": 25, "city": "New York"}
with open("data.json", "w") as file:
json.dump(data, file) # Convert Python dictionary to JSONwith open("data.json", "r") as file:
data = json.load(file) # Convert JSON to Python dictionary
print(data)To delete a file, use the os module.
import os
if os.path.exists("example.txt"):
os.remove("example.txt")
print("File deleted")
else:
print("File does not exist")