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.