Python File System Guide: 10 Essential Operations with Code Examples [2024]

Aarav Joshi - Feb 14 - - Dev Community

As a best-selling author, I invite you to explore my books on Amazon. Don't forget to follow me on Medium and show your support. Thank you! Your support means the world!

Python File System Operations and Management

Python provides powerful tools for file system operations. I've spent years working with these techniques, and I'll share practical insights that have proven invaluable in real-world applications.

The pathlib module transforms how we handle file paths in Python. It offers an object-oriented interface that makes path manipulation intuitive and safe. Here's how I typically use it:

from pathlib import Path

# Create and manage paths
base_dir = Path('/home/user/documents')
file_path = base_dir / 'data' / 'report.txt'

# File operations
file_path.write_text('Hello World')
content = file_path.read_text()
stats = file_path.stat()

# Directory operations
new_dir = base_dir / 'new_folder'
new_dir.mkdir(parents=True, exist_ok=True)
Enter fullscreen mode Exit fullscreen mode

Monitoring file system changes is crucial for many applications. The watchdog library excels at this task:

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time

class ChangeHandler(FileSystemEventHandler):
    def on_modified(self, event):
        if not event.is_directory:
            print(f"Modified: {event.src_path}")

    def on_created(self, event):
        print(f"Created: {event.src_path}")

observer = Observer()
observer.schedule(ChangeHandler(), ".", recursive=True)
observer.start()

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    observer.stop()
observer.join()
Enter fullscreen mode Exit fullscreen mode

Memory-mapped files using mmap can significantly improve performance when dealing with large files:

import mmap
import os

def process_large_file(filename):
    with open(filename, 'r+b') as f:
        mm = mmap.mmap(f.fileno(), 0)
        # Process file in chunks
        chunk_size = 1024 * 1024  # 1MB chunks
        for i in range(0, len(mm), chunk_size):
            chunk = mm[i:i + chunk_size]
            # Process chunk
        mm.close()
Enter fullscreen mode Exit fullscreen mode

Temporary files are essential for many operations. The tempfile module provides secure ways to handle them:

import tempfile
import shutil

with tempfile.TemporaryDirectory() as temp_dir:
    temp_path = Path(temp_dir) / 'temp_file.txt'
    temp_path.write_text('Temporary data')
    # Process data
    # Directory and contents automatically cleaned up

with tempfile.NamedTemporaryFile(delete=False) as temp_file:
    temp_file.write(b'Binary data')
    temp_path = temp_file.name

# Manual cleanup when needed
Path(temp_path).unlink()
Enter fullscreen mode Exit fullscreen mode

The shutil module offers high-level operations for file management:

import shutil

# Copy operations
shutil.copy2('source.txt', 'destination.txt')
shutil.copytree('source_dir', 'backup_dir')

# Move operations
shutil.move('old_location.txt', 'new_location.txt')

# Remove directory tree
shutil.rmtree('temp_directory')

# Disk usage
total, used, free = shutil.disk_usage('/')
Enter fullscreen mode Exit fullscreen mode

fsspec provides a unified interface for different filesystem types:

import fsspec

# Local filesystem
with fsspec.open('file://path/to/local/file.txt', 'w') as f:
    f.write('Local file content')

# S3 storage
with fsspec.open('s3://bucket/file.txt', 'rb') as f:
    content = f.read()

# HTTP filesystem
with fsspec.open('https://example.com/data.json') as f:
    data = f.read()
Enter fullscreen mode Exit fullscreen mode

PyFilesystem offers abstract filesystem operations:

from fs import open_fs

# Create filesystem objects
local_fs = open_fs('.')
memory_fs = open_fs('mem://')

# Copy between filesystems
fs.copy.copy_file(
    local_fs, 'source.txt',
    memory_fs, 'destination.txt'
)

# List directory contents
for path in local_fs.walk.files():
    print(path)
Enter fullscreen mode Exit fullscreen mode

Here's a practical example combining multiple techniques for a file synchronization system:

from pathlib import Path
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import shutil
import time
import hashlib

class SyncHandler(FileSystemEventHandler):
    def __init__(self, source_dir, backup_dir):
        self.source_dir = Path(source_dir)
        self.backup_dir = Path(backup_dir)

    def get_file_hash(self, path):
        with open(path, 'rb') as f:
            return hashlib.md5(f.read()).hexdigest()

    def sync_file(self, src_path):
        rel_path = Path(src_path).relative_to(self.source_dir)
        dest_path = self.backup_dir / rel_path

        if not dest_path.parent.exists():
            dest_path.parent.mkdir(parents=True)

        if not dest_path.exists() or \
           self.get_file_hash(src_path) != self.get_file_hash(dest_path):
            shutil.copy2(src_path, dest_path)
            print(f"Synced: {rel_path}")

    def on_modified(self, event):
        if not event.is_directory:
            self.sync_file(event.src_path)

def start_sync(source_dir, backup_dir):
    handler = SyncHandler(source_dir, backup_dir)
    observer = Observer()
    observer.schedule(handler, source_dir, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
Enter fullscreen mode Exit fullscreen mode

Error handling is crucial in file operations. Here's a robust approach:

from contextlib import contextmanager
import os
import errno

@contextmanager
def safe_file_operation(path):
    temp_path = path + '.tmp'
    try:
        yield temp_path
        # Atomic operation on most systems
        os.replace(temp_path, path)
    except Exception as e:
        if os.path.exists(temp_path):
            os.unlink(temp_path)
        if isinstance(e, OSError) and e.errno == errno.ENOSPC:
            raise RuntimeError("No space left on device")
        raise

def write_data_safely(path, data):
    with safe_file_operation(path) as temp_path:
        with open(temp_path, 'w') as f:
            f.write(data)
Enter fullscreen mode Exit fullscreen mode

Cross-platform compatibility requires careful consideration:

import os
import platform

def get_app_data_dir():
    system = platform.system().lower()
    if system == 'windows':
        base_dir = os.environ.get('APPDATA')
    elif system == 'darwin':
        base_dir = os.path.expanduser('~/Library/Application Support')
    else:
        base_dir = os.path.expanduser('~/.local/share')

    app_dir = os.path.join(base_dir, 'myapp')
    os.makedirs(app_dir, exist_ok=True)
    return app_dir
Enter fullscreen mode Exit fullscreen mode

These techniques form the foundation of robust file system operations in Python. The key is to choose the right tool for each specific use case while maintaining code reliability and performance.

Remember to handle errors gracefully, ensure atomic operations where necessary, and consider platform-specific requirements. Regular testing across different operating systems helps maintain compatibility and reliability.

Through careful implementation of these techniques, you can build powerful file management systems that are both efficient and maintainable.


101 Books

101 Books is an AI-driven publishing company co-founded by author Aarav Joshi. By leveraging advanced AI technology, we keep our publishing costs incredibly low—some books are priced as low as $4—making quality knowledge accessible to everyone.

Check out our book Golang Clean Code available on Amazon.

Stay tuned for updates and exciting news. When shopping for books, search for Aarav Joshi to find more of our titles. Use the provided link to enjoy special discounts!

Our Creations

Be sure to check out our creations:

Investor Central | Investor Central Spanish | Investor Central German | Smart Living | Epochs & Echoes | Puzzling Mysteries | Hindutva | Elite Dev | JS Schools


We are on Medium

Tech Koala Insights | Epochs & Echoes World | Investor Central Medium | Puzzling Mysteries Medium | Science & Epochs Medium | Modern Hindutva

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .