Skip to main content

Python's os Module

The os module in Python provides a portable way to interact with the operating system. It offers functions for file and directory operations, environment variables, process management, and system information.

Directory Operations

Current Working Directory

import os

# Get current working directory
current_dir = os.getcwd()
print(f"Current directory: {current_dir}")

# Change working directory
os.chdir('/tmp')
print(f"New directory: {os.getcwd()}")

# Go back to previous directory
os.chdir(current_dir)

Creating and Removing Directories

# Create a single directory
os.mkdir('new_folder')

# Create nested directories
os.makedirs('parent/child/grandchild', exist_ok=True)

# Remove empty directory
os.rmdir('new_folder')

# Remove directory tree
os.removedirs('parent/child/grandchild')  # Removes if empty

# Alternative for non-empty directories
import shutil
shutil.rmtree('parent')  # Removes entire tree

Directory Listing and Traversal

# List directory contents
files = os.listdir('.')
print("Files in current directory:", files)

# Filter specific file types
python_files = [f for f in os.listdir('.') if f.endswith('.py')]
print("Python files:", python_files)

# Walk through directory tree
for root, dirs, files in os.walk('.'):
    print(f"Directory: {root}")
    for file in files:
        full_path = os.path.join(root, file)
        print(f"  File: {full_path}")

File Operations

File Existence and Information

filename = 'example.txt'

# Check if file exists
if os.path.exists(filename):
    print(f"{filename} exists")

# Check if it's a file or directory
print(f"Is file: {os.path.isfile(filename)}")
print(f"Is directory: {os.path.isdir(filename)}")
print(f"Is link: {os.path.islink(filename)}")

# Get file size
if os.path.exists(filename):
    size = os.path.getsize(filename)
    print(f"File size: {size} bytes")

File Operations

# Create a file
with open('test.txt', 'w') as f:
    f.write('Hello, World!')

# Rename file
os.rename('test.txt', 'renamed_test.txt')

# Copy file (requires shutil)
import shutil
shutil.copy2('renamed_test.txt', 'copy_of_test.txt')

# Remove file
os.remove('copy_of_test.txt')

# Alternative removal methods
os.unlink('renamed_test.txt')  # Same as remove

Path Manipulation

Path Components

path = '/home/user/documents/file.txt'

# Split path components
print(f"Directory: {os.path.dirname(path)}")
print(f"Filename: {os.path.basename(path)}")
print(f"Split: {os.path.split(path)}")

# File extension
name, ext = os.path.splitext('document.pdf')
print(f"Name: {name}, Extension: {ext}")

# Join paths (cross-platform)
new_path = os.path.join('home', 'user', 'documents', 'new_file.txt')
print(f"Joined path: {new_path}")

Absolute and Relative Paths

# Convert to absolute path
relative_path = './data/file.txt'
absolute_path = os.path.abspath(relative_path)
print(f"Absolute path: {absolute_path}")

# Get relative path
rel_path = os.path.relpath('/home/user/documents/file.txt', '/home/user')
print(f"Relative path: {rel_path}")

# Normalize path
messy_path = '/home/user/../user/./documents//file.txt'
clean_path = os.path.normpath(messy_path)
print(f"Normalized: {clean_path}")

Environment Variables

Reading Environment Variables

# Get specific environment variable
home_dir = os.environ.get('HOME', 'Not found')
print(f"Home directory: {home_dir}")

# Get with default value
python_path = os.environ.get('PYTHONPATH', '/usr/local/python')
print(f"Python path: {python_path}")

# Get all environment variables
for key, value in os.environ.items():
    if 'PATH' in key:
        print(f"{key}: {value}")

Setting Environment Variables

# Set environment variable for current process
os.environ['MY_VARIABLE'] = 'Hello World'
print(os.environ.get('MY_VARIABLE'))

# Using putenv (alternative method)
os.putenv('ANOTHER_VAR', 'Another Value')

# Delete environment variable
if 'MY_VARIABLE' in os.environ:
    del os.environ['MY_VARIABLE']

Process Management

Process Information

# Get process ID
print(f"Current process ID: {os.getpid()}")
print(f"Parent process ID: {os.getppid()}")

# Get user and group IDs (Unix/Linux)
try:
    print(f"User ID: {os.getuid()}")
    print(f"Group ID: {os.getgid()}")
except AttributeError:
    print("User/Group IDs not available on Windows")

Executing Commands

# Execute system command
return_code = os.system('echo "Hello from system command"')
print(f"Command returned: {return_code}")

# Execute command and capture output
import subprocess
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)
print("Command output:", result.stdout)

# Using os.popen (deprecated but still available)
with os.popen('date') as pipe:
    output = pipe.read()
    print(f"Date output: {output.strip()}")

File Permissions and Stats

File Statistics

filename = 'example.txt'
if os.path.exists(filename):
    stat_info = os.stat(filename)
    print(f"File size: {stat_info.st_size}")
    print(f"Last modified: {stat_info.st_mtime}")
    print(f"Last accessed: {stat_info.st_atime}")
    print(f"Created: {stat_info.st_ctime}")
    print(f"Mode: {oct(stat_info.st_mode)}")

Changing Permissions

# Change file permissions (Unix/Linux)
try:
    os.chmod('example.txt', 0o755)  # rwxr-xr-x
    print("Permissions changed successfully")
except OSError as e:
    print(f"Permission change failed: {e}")

# Change ownership (Unix/Linux, requires privileges)
try:
    os.chown('example.txt', 1000, 1000)  # uid, gid
except (OSError, AttributeError) as e:
    print(f"Ownership change failed: {e}")

Platform-Specific Information

System Information

# Get platform information
print(f"Operating system: {os.name}")
print(f"Platform: {os.uname() if hasattr(os, 'uname') else 'Windows'}")

# Path separator
print(f"Path separator: '{os.sep}'")
print(f"Alternative separator: '{os.altsep}'")
print(f"Path list separator: '{os.pathsep}'")
print(f"Line separator: {repr(os.linesep)}")

# Current user
print(f"Current user: {os.getlogin()}")

Advanced Examples

Directory Tree Creator

def create_project_structure(base_path):
    """Create a typical project directory structure"""
    directories = [
        'src',
        'tests',
        'docs',
        'data/raw',
        'data/processed',
        'config',
        'logs'
    ]
    
    for directory in directories:
        full_path = os.path.join(base_path, directory)
        os.makedirs(full_path, exist_ok=True)
        print(f"Created: {full_path}")

# Usage
create_project_structure('my_project')

File Finder

def find_files(directory, extension, recursive=True):
    """Find all files with specific extension"""
    found_files = []
    
    if recursive:
        for root, dirs, files in os.walk(directory):
            for file in files:
                if file.endswith(extension):
                    found_files.append(os.path.join(root, file))
    else:
        for file in os.listdir(directory):
            if file.endswith(extension) and os.path.isfile(os.path.join(directory, file)):
                found_files.append(os.path.join(directory, file))
    
    return found_files

# Usage
python_files = find_files('.', '.py')
print("Found Python files:", python_files)

Disk Usage Calculator

def get_directory_size(path):
    """Calculate total size of directory"""
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            file_path = os.path.join(dirpath, filename)
            try:
                total_size += os.path.getsize(file_path)
            except (OSError, FileNotFoundError):
                pass
    return total_size

# Usage
size = get_directory_size('.')
print(f"Directory size: {size / (1024*1024):.2f} MB")

The os module is fundamental for system-level operations in Python, providing cross-platform compatibility and extensive functionality for file system interaction, process management, and environment manipulation.

Popular posts from this blog

How to Check if Someone is Connected to Your Machine in Linux

In today's tech-savvy world, securing your machine is more crucial than ever. Imagine finding out that someone else is accessing your files or using your resources without permission. It’s unnerving, right? If you’re a Linux user, knowing how to check for unauthorized connections can help you safeguard your system. Here’s a straightforward guide on how to spot if someone is connected to your Linux machine. Understanding Network Connections Before jumping into the steps, let's get a grasp of what network connections mean. Every device connected to the internet has an IP address. When another user connects to your machine, they do it through this address. This connection could happen through various means, such as a direct network connection or even over the internet. Recognizing established connections is essential. Think of it like keeping an eye on who enters your home. You want to know who’s coming and going at all times, right? Using the netstat Command One of the most...

JDBC SSL Connection: A Step-by-Step Guide for Secure Java Apps

Picture this: you're working on a Java application, and it needs to communicate with a database. That's where JDBC, which stands for Java Database Connectivity, comes into play. It's a key part of Java's ecosystem for managing database connections.  Think of JDBC as a translator between your Java application and a database, allowing you to perform tasks like querying, updating, and managing your data directly from your code.  It's the bridge that enables SQL commands from Java to get executed in your database, and it plays nice with most SQL databases out there. Key Features of JDBC Understanding JDBC's features can help you make the most of it for your database connections: Platform Independence : JDBC helps you write database applications that work on any operating system. If your app runs on Java, it can use JDBC. SQL Compatibility : It lets Java applications interact with standard SQL databases. This means any data manipulation you perform is consistent...

Layer 1 vs Layer 2 in the OSI Model: What's the Difference?

The OSI Model (Open Systems Interconnection Model) is like a blueprint for how computers communicate over a network.  It was created to standardize networking protocols, ensuring that different systems could connect and communicate with each other smoothly.  Picture it as a seven-layer cake, where each layer has a unique job but all work together to deliver data from one place to another.  This model helps developers and IT professionals understand and troubleshoot network communication by breaking down its complex processes. Overview of the Seven Layers Let's explore each layer and see what it does! Here's a breakdown: Physical Layer : The foundation of our network cake! This layer deals with the physical connection between devices — wires, cables, and all. Think of it as the roads on which your data traffic travels. Data Link Layer : Like traffic lights, this layer controls who can send data at what time to avoid collisions. It also packages your data into neat...