Important Points for File Handling in Python

File handling is a crucial skill in Python programming. Below are key points and best practices to ensure safe and efficient file operations.

Key Points

Best Practices Examples

Example 1: Using with statement to read a file

with open('data.txt', 'r', encoding='utf-8') as file:
    content = file.read()
print(content)

Example 2: Writing to a file with exception handling

try:
    with open('output.txt', 'w', encoding='utf-8') as file:
        file.write("Hello, world!")
except IOError as e:
    print("File error:", e)

Example 3: Using pathlib for paths

from pathlib import Path
file_path = Path('example.txt')
if file_path.exists():
    print("File exists.")

Example 4: Reading a large file line by line

with open('large_file.txt', 'r') as file:
    for line in file:
        print(line.strip())

Example 5: Appending to a file

with open('log.txt', 'a') as file:
    file.write("New log entry\n")

Example 6: Using tempfile for a temporary file

import tempfile
with tempfile.TemporaryFile() as temp_file:
    temp_file.write(b'Hello temp')
    temp_file.seek(0)
    print(temp_file.read())

Example 7: Checking file permissions

import os
filepath = 'example.txt'
if os.access(filepath, os.W_OK):
    print("File is writable")

Example 8: Safely renaming a file

import os
os.rename('oldname.txt', 'newname.txt')

Example 9: Deleting a file with error handling

import os
try:
    os.remove('tempfile.txt')
except FileNotFoundError:
    print("File not found.")

Example 10: Writing JSON to a file

import json
data = {'name': 'Alice', 'age': 30}
with open('data.json', 'w') as file:
    json.dump(data, file)