Code-Memo

File Handling and Input/Output Operations

Working with Binary Data

Python allows you to read and write binary data using file objects opened in binary mode ('rb' for reading and 'wb' for writing).

with open('binary_file.bin', 'rb') as file:
    data = file.read()
    # Process binary data
data = b'\x48\x65\x6c\x6c\x6f'  # Example binary data
with open('binary_file.bin', 'wb') as file:
    file.write(data)

Contextlib and Pathlib Modules

contextlib Module

The contextlib module provides utilities for working with context managers, simplifying the creation of context managers using generators and decorators.

import contextlib

@contextlib.contextmanager
def open_file(filename):
    file = open(filename, 'r')
    try:
        yield file
    finally:
        file.close()

# Usage:
with open_file('example.txt') as file:
    content = file.read()
    print(content)
pathlib Module

The pathlib module provides an object-oriented approach to working with filesystem paths. It offers a more readable and convenient way to manipulate files and directories compared to traditional string-based operations.

from pathlib import Path

# Read file content
file_path = Path('example.txt')
content = file_path.read_text()
print(content)

# Write to file
new_content = 'New content'
file_path.write_text(new_content)

File Locking and Atomic Writes

File Locking

File locking ensures that a file is not accessed or modified by multiple processes simultaneously, preventing data corruption or conflicts.

import fcntl

with open('data.txt', 'a') as file:
    fcntl.flock(file, fcntl.LOCK_EX)  # Acquire exclusive lock
    file.write('New data\n')
    fcntl.flock(file, fcntl.LOCK_UN)  # Release lock
Atomic Writes

Atomic writes ensure that a file write operation is either fully completed or not at all, avoiding partial writes or corrupted data in case of interruptions.

import os
import tempfile

filename = 'data.txt'
temp_filename = filename + '.tmp'

# Write data to temporary file
with open(temp_filename, 'w') as temp_file:
    temp_file.write('New data\n')

# Atomic write by renaming temporary file
os.replace(temp_filename, filename)

Benefits of File Handling and I/O Operations