Docs
real development
12 - Real Development Modules
📌 What You'll Learn
- •
osmodule - Operating system interface - •
sysmodule - System-specific parameters - •
loggingmodule - Application logging - •
argparsemodule - Command-line arguments - •
pathlibmodule - Modern path handling
📁 os Module
The os module provides a way to interact with the operating system.
Directory Operations
import os
# Current working directory
cwd = os.getcwd()
print(f"Current directory: {cwd}")
# Change directory
os.chdir('/tmp')
# List directory contents
files = os.listdir('.') # Current directory
files = os.listdir('/home') # Specific path
# Create directory
os.mkdir('new_folder') # Single directory
os.makedirs('path/to/nested/folder', exist_ok=True) # Nested
# Remove directory
os.rmdir('empty_folder')
os.removedirs('path/to/nested/folder') # Remove nested empty dirs
# Check if path exists
os.path.exists('/path/to/file')
os.path.isfile('/path/to/file')
os.path.isdir('/path/to/folder')
File Operations
import os
# Rename file/directory
os.rename('old_name.txt', 'new_name.txt')
# Remove file
os.remove('file_to_delete.txt')
# Get file info
info = os.stat('file.txt')
print(f"Size: {info.st_size} bytes")
print(f"Modified: {info.st_mtime}")
# Walk through directory tree
for root, dirs, files in os.walk('/path'):
print(f"Directory: {root}")
for file in files:
print(f" File: {file}")
Path Operations
import os.path
# Join paths (cross-platform)
path = os.path.join('folder', 'subfolder', 'file.txt')
# Split path components
dirname = os.path.dirname('/path/to/file.txt') # '/path/to'
basename = os.path.basename('/path/to/file.txt') # 'file.txt'
name, ext = os.path.splitext('file.txt') # ('file', '.txt')
# Absolute path
abs_path = os.path.abspath('relative/path')
# Expand user (~)
home = os.path.expanduser('~')
Environment Variables
import os
# Get environment variable
home = os.environ.get('HOME')
path = os.environ.get('PATH', 'default_value')
# Set environment variable
os.environ['MY_VAR'] = 'value'
# Get all environment variables
for key, value in os.environ.items():
print(f"{key}={value}")
⚙️ sys Module
The sys module provides access to Python interpreter variables and functions.
import sys
# Command-line arguments
print(sys.argv) # List of arguments
script_name = sys.argv[0]
if len(sys.argv) > 1:
first_arg = sys.argv[1]
# Python version
print(sys.version)
print(sys.version_info)
# Platform
print(sys.platform) # 'linux', 'darwin', 'win32'
# Python path
print(sys.path) # Module search paths
sys.path.append('/custom/path')
# Exit program
sys.exit(0) # Exit with status code
# Standard streams
sys.stdout.write("Hello\n")
sys.stderr.write("Error\n")
# Object size
size = sys.getsizeof([1, 2, 3])
print(f"List size: {size} bytes")
# Recursion limit
print(sys.getrecursionlimit())
sys.setrecursionlimit(2000)
📝 logging Module
Professional way to track events in your application.
Basic Logging
import logging
# Basic configuration
logging.basicConfig(level=logging.DEBUG)
# Log messages at different levels
logging.debug("Debug message") # Detailed info for debugging
logging.info("Info message") # Confirmation that things work
logging.warning("Warning message") # Something unexpected
logging.error("Error message") # More serious problem
logging.critical("Critical!") # Very serious error
Logging Configuration
import logging
# Configure logging format
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
# Log to file
logging.basicConfig(
filename='app.log',
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
Using Logger Objects
import logging
# Create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Create handlers
console_handler = logging.StreamHandler()
file_handler = logging.FileHandler('app.log')
# Set levels for handlers
console_handler.setLevel(logging.WARNING)
file_handler.setLevel(logging.DEBUG)
# Create formatters
formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
# Add handlers to logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# Use logger
logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
Logging Exceptions
import logging
logger = logging.getLogger(__name__)
try:
result = 10 / 0
except ZeroDivisionError:
logger.exception("An error occurred") # Includes traceback
🖥️ argparse Module
Parse command-line arguments professionally.
Basic Usage
import argparse
# Create parser
parser = argparse.ArgumentParser(description='My awesome program')
# Add arguments
parser.add_argument('filename', help='Input file name')
parser.add_argument('-o', '--output', help='Output file name')
parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output')
parser.add_argument('-n', '--number', type=int, default=10, help='Number of items')
# Parse arguments
args = parser.parse_args()
# Use arguments
print(f"Input: {args.filename}")
print(f"Output: {args.output}")
print(f"Verbose: {args.verbose}")
print(f"Number: {args.number}")
Argument Types
import argparse
parser = argparse.ArgumentParser()
# Positional arguments (required)
parser.add_argument('name', help='Your name')
# Optional arguments
parser.add_argument('-a', '--age', type=int, help='Your age')
# Boolean flags
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('--no-cache', action='store_false', dest='cache')
# Choices
parser.add_argument('--color', choices=['red', 'green', 'blue'])
# Multiple values
parser.add_argument('--files', nargs='+', help='Input files')
parser.add_argument('--coords', nargs=2, type=float, help='X Y coordinates')
# Required optional argument
parser.add_argument('--config', required=True)
# Default values
parser.add_argument('--port', type=int, default=8080)
args = parser.parse_args()
Subcommands
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='command')
# 'init' subcommand
init_parser = subparsers.add_parser('init', help='Initialize project')
init_parser.add_argument('--template', default='basic')
# 'build' subcommand
build_parser = subparsers.add_parser('build', help='Build project')
build_parser.add_argument('--output', '-o')
# 'run' subcommand
run_parser = subparsers.add_parser('run', help='Run project')
run_parser.add_argument('--port', type=int, default=8000)
args = parser.parse_args()
if args.command == 'init':
print(f"Initializing with template: {args.template}")
elif args.command == 'build':
print(f"Building to: {args.output}")
elif args.command == 'run':
print(f"Running on port: {args.port}")
📂 pathlib Module (Modern Path Handling)
pathlib provides an object-oriented way to handle filesystem paths.
from pathlib import Path
# Create path objects
path = Path('/home/user/documents')
current = Path.cwd()
home = Path.home()
# Path operations
new_path = path / 'subfolder' / 'file.txt'
# Path components
print(path.name) # 'documents'
print(path.parent) # '/home/user'
print(path.suffix) # '.txt' for a file
print(path.stem) # filename without extension
# Check path properties
path.exists()
path.is_file()
path.is_dir()
path.is_absolute()
# Create directories
Path('new/nested/dir').mkdir(parents=True, exist_ok=True)
# Read/write files
content = Path('file.txt').read_text()
Path('output.txt').write_text('Hello, World!')
# Iterate directory
for file in Path('.').iterdir():
print(file)
# Glob patterns
for py_file in Path('.').glob('*.py'):
print(py_file)
for py_file in Path('.').rglob('*.py'): # Recursive
print(py_file)
📋 Summary
| Module | Purpose |
|---|---|
os | OS operations (files, dirs, env) |
sys | Python interpreter & runtime |
logging | Application logging |
argparse | Command-line arguments |
pathlib | Modern path handling |
🎯 Next Steps
After mastering real development modules, proceed to 13_regex to learn about pattern matching with regular expressions!