The os module provides operating system interfaces. Here are the essentials.
Environment Variables
import os
# Get variable
home = os.environ.get("HOME")
api_key = os.environ.get("API_KEY", "default")
# Set variable (current process only)
os.environ["MY_VAR"] = "value"
# Check if exists
if "DEBUG" in os.environ:
debug_mode = True
# Get all
for key, value in os.environ.items():
print(f"{key}={value}")Current Directory
# Get current directory
cwd = os.getcwd()
# Change directory
os.chdir("/path/to/dir")
# Change and return (use contextlib for temp changes)
from contextlib import contextmanager
@contextmanager
def cd(path):
old = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(old)
with cd("/tmp"):
# Work in /tmp
pass
# Back to original directoryPath Operations
Note: Prefer pathlib for modern code, but os.path is still useful.
import os.path
# Join paths
path = os.path.join("dir", "subdir", "file.txt")
# Split
dirname, filename = os.path.split("/path/to/file.txt")
# ("/path/to", "file.txt")
name, ext = os.path.splitext("file.txt")
# ("file", ".txt")
# Absolute path
os.path.abspath("relative/path")
# Normalize path
os.path.normpath("/path/../other/./file") # "/other/file"
# Expand user
os.path.expanduser("~/documents") # "/home/user/documents"
# Check paths
os.path.exists("/path/to/file")
os.path.isfile("/path/to/file")
os.path.isdir("/path/to/dir")
os.path.isabs("/absolute/path")Directory Operations
# List directory
files = os.listdir(".")
files = os.listdir("/path/to/dir")
# Create directory
os.mkdir("new_dir")
os.makedirs("path/to/nested/dir", exist_ok=True)
# Remove directory
os.rmdir("empty_dir") # Must be empty
# Remove tree (use shutil)
import shutil
shutil.rmtree("dir_with_contents")File Operations
# Rename/move
os.rename("old_name.txt", "new_name.txt")
# Remove file
os.remove("file.txt")
os.unlink("file.txt") # Same thing
# File info
stat = os.stat("file.txt")
stat.st_size # Size in bytes
stat.st_mtime # Modification time
stat.st_mode # Permissions
# Check permissions
os.access("file.txt", os.R_OK) # Readable?
os.access("file.txt", os.W_OK) # Writable?
os.access("file.txt", os.X_OK) # Executable?
# Change permissions
os.chmod("script.sh", 0o755)Walking Directory Trees
# Walk all files recursively
for root, dirs, files in os.walk("."):
for file in files:
full_path = os.path.join(root, file)
print(full_path)
# Skip certain directories
if ".git" in dirs:
dirs.remove(".git")System Information
# Platform
os.name # "posix" or "nt"
os.uname() # System info (Unix)
# User info
os.getuid() # User ID (Unix)
os.getlogin() # Login name
# Process info
os.getpid() # Current process ID
os.getppid() # Parent process ID
# CPU count
os.cpu_count()Temporary Files
import tempfile
# Get temp directory
temp_dir = tempfile.gettempdir()
# Create temp file
fd, path = tempfile.mkstemp(suffix=".txt")
os.close(fd) # Close file descriptor
# Create temp directory
temp_dir = tempfile.mkdtemp()
# Context manager (auto-cleanup)
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(b"data")
temp_path = f.namePath Separators
os.sep # "/" on Unix, "\\" on Windows
os.pathsep # ":" on Unix, ";" on Windows
os.linesep # "\n" on Unix, "\r\n" on WindowsRunning Commands
Note: Prefer subprocess for complex cases.
# Simple command (deprecated, use subprocess)
os.system("ls -la")
# Get output
stream = os.popen("ls")
output = stream.read()Common Patterns
Safe file removal
def safe_remove(path):
try:
os.remove(path)
except FileNotFoundError:
passEnsure directory exists
def ensure_dir(path):
os.makedirs(path, exist_ok=True)Find files by extension
def find_files(directory, extension):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(extension):
yield os.path.join(root, file)
for py_file in find_files(".", ".py"):
print(py_file)Get file size
def get_size(path):
if os.path.isfile(path):
return os.path.getsize(path)
total = 0
for root, dirs, files in os.walk(path):
for file in files:
total += os.path.getsize(os.path.join(root, file))
return totalQuick Reference
import os
# Environment
os.environ.get("VAR")
os.environ["VAR"] = "value"
# Paths
os.path.join("a", "b")
os.path.exists(path)
os.path.isfile(path)
os.path.isdir(path)
# Directories
os.getcwd()
os.chdir(path)
os.listdir(path)
os.makedirs(path, exist_ok=True)
# Files
os.remove(path)
os.rename(old, new)
os.stat(path)
# Walking
for root, dirs, files in os.walk(path):
...
# System
os.name
os.cpu_count()
os.getpid()For path manipulation, prefer pathlib. Use os for environment variables and system interaction.
React to this post: