Python os Module

The os module provides a portable way to use operating system-dependent functionality. It offers functions for interacting with the file system, environment variables, process management, and more.

Use Cases:
- Reading and setting environment variables
- Creating and removing directories
- Walking directory trees to find files
- Getting information about the current process (PID, user)
- Executing system commands (though subprocess is preferred)

Example 1: Working with environment variables
    import os

    # Read environment variables with defaults
    db_host = os.environ.get("DATABASE_HOST", "localhost")
    db_port = int(os.environ.get("DATABASE_PORT", "5432"))
    api_key = os.environ.get("API_KEY")
    if not api_key:
        raise ValueError("API_KEY environment variable is required")
    print(f"Connecting to {db_host}:{db_port}")

Example 2: Walking a directory tree
    import os

    for root, dirs, files in os.walk("/home/user/project"):
        # Skip hidden directories
        dirs[:] = [d for d in dirs if not d.startswith(".")]
        for filename in files:
            if filename.endswith(".py"):
                filepath = os.path.join(root, filename)
                size = os.path.getsize(filepath)
                print(f"{filepath}: {size} bytes")

While pathlib is preferred for path manipulation in modern Python, the os module remains essential for environment variable access, process information, and low-level OS interactions. The os.path submodule provides path manipulation functions that predate pathlib.
