Essential Python file operations for reading, writing, searching, and manipulating files. Perfect for debugging, data processing, and automation tasks.
# Read entire file
with open('filename.txt', 'r') as f:
content = f.read()
# Read line by line
with open('filename.txt', 'r') as f:
for line in f:
print(line.strip())
# Read all lines into a list
with open('filename.txt', 'r') as f:
lines = f.readlines()
# Read with error handling
try:
with open('filename.txt', 'r') as f:
content = f.read()
except FileNotFoundError:
print("File not found!")
except PermissionError:
print("Permission denied!")
# Read first N lines
with open('filename.txt', 'r') as f:
first_lines = [f.readline() for _ in range(5)]
# Read specific line number
def read_line(filename, line_number):
with open(filename, 'r') as f:
for i, line in enumerate(f, 1):
if i == line_number:
return line.strip()
return None
# Read file backwards (last lines first)
with open('filename.txt', 'r') as f:
lines = f.readlines()
for line in reversed(lines):
print(line.strip())
# Read with different encoding
with open('filename.txt', 'r', encoding='utf-8') as f:
content = f.read()
# Write (overwrite) file
with open('filename.txt', 'w') as f:
f.write("Hello World\n")
# Append to file
with open('filename.txt', 'a') as f:
f.write("New line\n")
# Write multiple lines
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
with open('filename.txt', 'w') as f:
f.writelines(lines)
# Write with formatting
data = {"name": "John", "age": 30}
with open('data.txt', 'w') as f:
f.write(f"Name: {data['name']}\n")
f.write(f"Age: {data['age']}\n")
import shutil
import os
def safe_write(filename, content):
# Create backup
if os.path.exists(filename):
shutil.copy(filename, f"{filename}.backup")
try:
with open(filename, 'w') as f:
f.write(content)
print(f"โ
Successfully wrote {filename}")
except Exception as e:
# Restore backup if write failed
if os.path.exists(f"{filename}.backup"):
shutil.copy(f"{filename}.backup", filename)
print(f"โ Write failed: {e}")
# Create file with timestamp
from datetime import datetime
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"data_{timestamp}.txt"
# Find lines containing specific text
def find_in_file(filename, search_text):
matches = []
with open(filename, 'r') as f:
for line_num, line in enumerate(f, 1):
if search_text in line:
matches.append((line_num, line.strip()))
return matches
# Usage
matches = find_in_file('ship_worker.py', 'def ')
for line_num, line in matches:
print(f"Line {line_num}: {line}")
# Case-insensitive search
def find_case_insensitive(filename, search_text):
search_text = search_text.lower()
matches = []
with open(filename, 'r') as f:
for line_num, line in enumerate(f, 1):
if search_text in line.lower():
matches.append((line_num, line.strip()))
return matches
# Find using regex
import re
def find_regex(filename, pattern):
matches = []
with open(filename, 'r') as f:
for line_num, line in enumerate(f, 1):
if re.search(pattern, line):
matches.append((line_num, line.strip()))
return matches
# Find function definitions
matches = find_regex('ship_worker.py', r'^def\s+\w+\(')
# Replace text in file
def replace_in_file(filename, old_text, new_text):
with open(filename, 'r') as f:
content = f.read()
# Replace text
new_content = content.replace(old_text, new_text)
with open(filename, 'w') as f:
f.write(new_content)
print(f"Replaced '{old_text}' with '{new_text}'")
# Replace with backup
def safe_replace(filename, old_text, new_text):
# Create backup
shutil.copy(filename, f"{filename}.backup")
try:
replace_in_file(filename, old_text, new_text)
except Exception as e:
# Restore backup on error
shutil.copy(f"{filename}.backup", filename)
print(f"Error: {e}. Restored from backup.")
# Replace only on specific lines
def replace_on_lines(filename, old_text, new_text, line_numbers):
with open(filename, 'r') as f:
lines = f.readlines()
for line_num in line_numbers:
if 1 <= line_num <= len(lines):
lines[line_num - 1] = lines[line_num - 1].replace(old_text, new_text)
with open(filename, 'w') as f:
f.writelines(lines)
# Replace using regex
import re
def regex_replace(filename, pattern, replacement):
with open(filename, 'r') as f:
content = f.read()
new_content = re.sub(pattern, replacement, content)
with open(filename, 'w') as f:
f.write(new_content)
# Replace tabs with spaces (like we did!)
def tabs_to_spaces(filename, spaces=4):
with open(filename, 'r') as f:
content = f.read()
new_content = content.replace('\t', ' ' * spaces)
with open(filename, 'w') as f:
f.write(new_content)
# Get specific line ranges
def get_lines(filename, start_line, end_line):
with open(filename, 'r') as f:
lines = f.readlines()
# Python uses 0-based indexing
return lines[start_line-1:end_line]
# Insert line at specific position
def insert_line(filename, line_number, new_line):
with open(filename, 'r') as f:
lines = f.readlines()
# Insert new line
lines.insert(line_number - 1, new_line + '\n')
with open(filename, 'w') as f:
f.writelines(lines)
# Delete specific lines
def delete_lines(filename, line_numbers):
with open(filename, 'r') as f:
lines = f.readlines()
# Sort in reverse to delete from end first
for line_num in sorted(line_numbers, reverse=True):
if 1 <= line_num <= len(lines):
del lines[line_num - 1]
with open(filename, 'w') as f:
f.writelines(lines)
# Count lines
def count_lines(filename):
with open(filename, 'r') as f:
return sum(1 for line in f)
# Remove empty lines
def remove_empty_lines(filename):
with open(filename, 'r') as f:
lines = f.readlines()
# Keep only non-empty lines
non_empty = [line for line in lines if line.strip()]
with open(filename, 'w') as f:
f.writelines(non_empty)
# Remove trailing whitespace
def remove_trailing_whitespace(filename):
with open(filename, 'r') as f:
lines = f.readlines()
# Strip trailing whitespace but keep newlines
cleaned = [line.rstrip() + '\n' for line in lines]
with open(filename, 'w') as f:
f.writelines(cleaned)
# Sort lines alphabetically
def sort_file_lines(filename):
with open(filename, 'r') as f:
lines = f.readlines()
# Sort lines (strip newlines for sorting, add back)
sorted_lines = sorted(line.strip() for line in lines)
with open(filename, 'w') as f:
f.write('\n'.join(sorted_lines) + '\n')
# Compare two files
def compare_files(file1, file2):
with open(file1, 'r') as f1, open(file2, 'r') as f2:
content1 = f1.read()
content2 = f2.read()
if content1 == content2:
print("โ
Files are identical")
return True
else:
print("โ Files are different")
return False
# Find differences line by line
def find_differences(file1, file2):
with open(file1, 'r') as f1, open(file2, 'r') as f2:
lines1 = f1.readlines()
lines2 = f2.readlines()
differences = []
max_lines = max(len(lines1), len(lines2))
for i in range(max_lines):
line1 = lines1[i] if i < len(lines1) else ""
line2 = lines2[i] if i < len(lines2) else ""
if line1 != line2:
differences.append((i+1, line1.strip(), line2.strip()))
return differences
# Check Python syntax
import py_compile
import sys
def check_syntax(filename):
try:
py_compile.compile(filename, doraise=True)
print(f"โ
{filename} syntax is valid")
return True
except py_compile.PyCompileError as e:
print(f"โ Syntax error in {filename}:")
print(e)
return False
# Check multiple Python files
import os
def check_all_python_files(directory='.'):
python_files = [f for f in os.listdir(directory) if f.endswith('.py')]
for file in python_files:
print(f"Checking {file}...")
check_syntax(file)
# Find Python imports
def find_imports(filename):
imports = []
with open(filename, 'r') as f:
for line_num, line in enumerate(f, 1):
line = line.strip()
if line.startswith('import ') or line.startswith('from '):
imports.append((line_num, line))
return imports
# Get file statistics
import os
from collections import Counter
def file_stats(filename):
with open(filename, 'r') as f:
content = f.read()
lines = content.split('\n')
stats = {
'file_size': os.path.getsize(filename),
'total_lines': len(lines),
'non_empty_lines': len([line for line in lines if line.strip()]),
'total_characters': len(content),
'total_words': len(content.split()),
}
return stats
# Count word frequency
def word_frequency(filename):
with open(filename, 'r') as f:
content = f.read().lower()
# Remove punctuation and split
import string
translator = str.maketrans('', '', string.punctuation)
words = content.translate(translator).split()
return Counter(words)
# Find longest lines
def find_longest_lines(filename, top_n=5):
with open(filename, 'r') as f:
lines = [(len(line), line_num, line.strip())
for line_num, line in enumerate(f, 1)]
# Sort by length, descending
longest = sorted(lines, reverse=True)[:top_n]
for length, line_num, content in longest:
print(f"Line {line_num} ({length} chars): {content[:50]}...")
# Extract errors from log file
def extract_errors(log_file):
errors = []
with open(log_file, 'r') as f:
for line_num, line in enumerate(f, 1):
if 'ERROR' in line or 'CRITICAL' in line:
errors.append((line_num, line.strip()))
return errors
# Find recent log entries
from datetime import datetime, timedelta
def find_recent_logs(log_file, hours=24):
recent_entries = []
cutoff = datetime.now() - timedelta(hours=hours)
with open(log_file, 'r') as f:
for line in f:
# Assuming log format: YYYY-MM-DD HH:MM:SS message
try:
timestamp_str = line[:19]
timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S')
if timestamp > cutoff:
recent_entries.append(line.strip())
except:
continue
return recent_entries
# Read .env file
def read_env_file(filename='.env'):
config = {}
try:
with open(filename, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'):
key, value = line.split('=', 1)
config[key] = value
except FileNotFoundError:
print(f"Config file {filename} not found")
return config
# Update .env file
def update_env_file(filename, key, value):
lines = []
key_found = False
try:
with open(filename, 'r') as f:
lines = f.readlines()
except FileNotFoundError:
pass
# Update existing key or add new one
with open(filename, 'w') as f:
for line in lines:
if line.startswith(f"{key}="):
f.write(f"{key}={value}\n")
key_found = True
else:
f.write(line)
# Add new key if not found
if not key_found:
f.write(f"{key}={value}\n")
# Count lines in file
lines = sum(1 for line in open('filename.txt'))
# Get file size
import os; size = os.path.getsize('filename.txt')
# Read file into list
lines = [line.strip() for line in open('filename.txt')]
# Write list to file
open('output.txt', 'w').write('\n'.join(my_list))
# Check if file exists
import os; exists = os.path.exists('filename.txt')
# Get file extension
import os; ext = os.path.splitext('filename.txt')[1]
# Read JSON file
import json; data = json.load(open('data.json'))
# Write JSON file
import json; json.dump(data, open('data.json', 'w'), indent=2)
with open()
- automatically closes filesencoding='utf-8'
when needed