Complete reference for Python file operations, modes, and methods. This covers all aspects of file handling including opening, reading, writing, and managing files and directories.
# Basic file reading
with open('data.txt', 'w') as f:
f.write("Hello World\nSecond Line")
with open('data.txt', 'r') as f:
content = f.read()
print(content)
# Basic file modes
with open('data.txt', 'r') as f: # Read text file
content = f.read()
with open('data.txt', 'w') as f: # Write text file (overwrites)
f.write("Hello World")
with open('data.txt', 'a') as f: # Append to text file
f.write("New line")
# Binary modes
with open('image.jpg', 'rb') as f: # Read binary file
data = f.read()
with open('output.jpg', 'wb') as f: # Write binary file
f.write(data)
# Read and write modes
with open('data.txt', 'r+') as f: # Read and write existing file
content = f.read()
f.write("Additional text")
# Exclusive creation
try:
with open('new_file.txt', 'x') as f: # Create new file (fails if exists)
f.write("New file content")
except FileExistsError:
print("File already exists")
# Read with position tracking
with open('data.txt', 'w') as f:
f.write("Hello World!")
with open('data.txt', 'r') as f:
print(f"Position: {f.tell()}") # 0
first_5 = f.read(5)
print(f"Read: {first_5}")
print(f"Position: {f.tell()}") # 5
# Read entire file
with open('data.txt', 'r') as f:
content = f.read()
print(content)
# Read specific number of characters
with open('data.txt', 'r') as f:
chunk = f.read(100) # Read first 100 characters
# Read line by line
with open('data.txt', 'r') as f:
for line in f: # Most efficient way to iterate
print(line.strip())
# Read all lines into list
with open('data.txt', 'r') as f:
lines = f.readlines()
for line in lines:
print(line.rstrip('\n'))
# Read with position tracking
with open('data.txt', 'r') as f:
print(f"Position: {f.tell()}") # 0
first_10 = f.read(10)
print(f"Position: {f.tell()}") # 10
f.seek(0) # Back to beginning
print(f"Position: {f.tell()}") # 0
# Append to existing file
with open('log.txt', 'a') as f:
f.write(f"[{datetime.now()}] New log entry\n")
# Append with data preservation
def log_message(message, filename='app.log'):
with open(filename, 'a', encoding='utf-8') as f:
timestamp = datetime.now().isoformat()
f.write(f"[{timestamp}] {message}\n")
log_message("Application started")
log_message("User login successful")
# Process large files efficiently
def process_large_file(filename, chunk_size=8192):
"""Process large file in chunks to avoid memory issues"""
try:
with open(filename, 'r') as f:
while True:
chunk = f.read(chunk_size)
if not chunk:
break
# Process chunk
process_chunk(chunk)
except Exception as e:
print(f"Error processing large file: {e}")
def process_chunk(chunk):
# Your processing logic here
pass
# Line-by-line processing for text files
def process_text_file_lines(filename):
"""Efficiently process text file line by line"""
try:
with open(filename, 'r') as f:
for line_number, line in enumerate(f, 1):
# Process each line
processed_line = line.strip().upper()
print(f"Line {line_number}: {processed_line}")
except Exception as e:
print(f"Error processing file lines: {e}")