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

Picture this: you glance at your system monitor and notice your CPU is humming along even though you're not running anything demanding. Or maybe your internet feels sluggish for no obvious reason. A small, uneasy thought creeps in — is someone else on my machine right now? For Linux users, this isn't something you have to wonder about. Linux ships with a powerful set of built-in tools that let you see exactly who's connected, who's logged in, and what your network is doing at any given moment. You don't need to be a security expert to use them — you just need to know where to look. This guide walks you through the practical, no-nonsense steps to check for unauthorized connections on your Linux system, with real commands you can run right now. Why Monitoring Network Connections Matters Every device on a network — including your own Linux machine — communicates using an IP address. When another device or user connects to your system, that connection shows up as a trac...

How to Set Up a Linux Web Server and Host an HTML Page Easily

Setting up a web server on Linux means spending a fair amount of time in the terminal — Linux leans heavily on the command line rather than clicking through menus, so you'll be typing out instructions more often than not.  If you're new to this, it can feel a little intimidating at first, but the good news is you don't need to become a Linux wizard overnight. A handful of core commands will get you surprisingly far. A few you'll lean on constantly: cd — move between directories ls — see what's in the current directory mkdir — create a new folder nano or vim — edit files right there in the terminal sudo — run something with administrator privileges Get comfortable with these and you'll be able to navigate around, tweak configuration files, and install software without much trouble. You don't need to memorize everything — you just need to be confident enough to follow along with clear instructions, which is exactly what this guide aims to give you....

Linux Network Troubleshooting

If you've spent any time as a sysadmin — or honestly, just as someone who's had to fix their own home network at 11pm — you know that connectivity issues are one of the most common headaches out there. The good news is that a handful of core tools and a methodical approach can take you from "why isn't this working" to a root cause pretty quickly.  This guide walks through the essentials: configuring interfaces, managing routes, and diagnosing problems when things go sideways. Configuring Network Interfaces Your network interfaces are the actual bridge between your machine and the outside world, so getting them configured correctly is step one for any kind of reliable connectivity. Doing It Manually ifconfig is the old-school, tried-and-true tool for this on Unix-like systems. To see everything currently configured, run: ifconfig -a If you need to manually set up a specific interface — assigning an IP, a netmask, and bringing it online — it looks like this: ifconf...