- Courses
- 💻Programming Languages
- 🐍Python
- Standard Library Deep Dive
- OS & Filesystem
On this page
OS & Filesystem
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
Purpose of This Lesson
This lesson provides a deep, practical understanding of how Python interacts with the operating system.
You will learn how to:
- Navigate the filesystem
- Inspect and manipulate files and directories
- Work with absolute and relative paths
- Use environment variables
- Perform safe, cross‑platform operations
- Apply modern best practices using
pathlib
This knowledge is essential for automation, data engineering, DevOps, and any program that touches the filesystem.
Working with the os Module
The os module provides low‑level access to operating‑system functionality.
Getting the Current Working Directory
import os
cwd = os.getcwd()
Changing Directories
os.chdir("/path/to/folder")
Listing Directory Contents
files = os.listdir(".")
Creating and Removing Directories
os.mkdir("data")
os.makedirs("logs/archive") # creates nested directories
os.rmdir("data") # removes empty directory
Checking File/Directory Existence
os.path.exists("config.json")
os.path.isfile("notes.txt")
os.path.isdir("images")
These operations are foundational but verbose, which is why modern Python prefers pathlib.
Modern Filesystem Handling with pathlib
The pathlib module provides an object‑oriented, cross‑platform API for filesystem paths.
Creating Path Objects
from pathlib import Path
p = Path("data/info.txt")
Joining Paths
full = Path("data") / "info.txt"
Checking Existence
p.exists()
p.is_file()
p.is_dir()
Reading and Writing Files
text = p.read_text(encoding="utf-8")
p.write_text("Hello", encoding="utf-8")
Iterating Through Directories
for entry in Path("logs").iterdir():
print(entry)
Creating Directories
Path("output").mkdir(exist_ok=True)
pathlib is the recommended approach for all new Python code.
Absolute vs. Relative Paths
Understanding path types is essential for predictable behavior.
Relative Path
Path("data/info.txt")
Interpreted relative to the current working directory.
Absolute Path
Path("/home/enrico/data/info.txt")
Always points to the same location, regardless of where the script runs.
Use absolute paths for reliability in automation and production systems.
Inspecting File Metadata
Python can retrieve detailed information about files.
Using os.stat()
import os
info = os.stat("data.txt")
Provides:
- Size
- Creation time
- Modification time
- Permissions
Using pathlib
p = Path("data.txt")
size = p.stat().st_size
modified = p.stat().st_mtime
This is useful for logging, auditing, and file‑processing pipelines.
Environment Variables
Environment variables store configuration outside your code.
Reading Environment Variables
import os
token = os.getenv("API_TOKEN")
Setting Environment Variables (Process‑Local)
os.environ["MODE"] = "development"
Environment variables are essential for secure configuration management.
Executing System Commands
Python can run external commands using os.system or subprocess.
Basic Example
os.system("ls -l")
Recommended: subprocess
import subprocess
result = subprocess.run(["ls", "-l"], capture_output=True, text=True)
subprocess provides safety, control, and portability.
Copying, Moving, and Deleting Files with shutil
The shutil module handles high‑level file operations.
Copying
import shutil
shutil.copy("source.txt", "backup.txt")
Moving
shutil.move("old.txt", "archive/old.txt")
Removing Directories
shutil.rmtree("temp_folder")
Use these operations carefully, as they modify the filesystem.
Safe Filesystem Practices
- Prefer
pathlibfor all new code - Always check existence before reading or writing
- Use
try/exceptfor error handling - Avoid hard‑coding absolute paths
- Use environment variables for configuration
- Never delete or overwrite files without explicit checks
- Log filesystem operations in production systems
These practices prevent data loss and improve reliability.
Summary
This lesson introduced the essential tools for interacting with the operating system and filesystem in Python.
You learned how to:
- Navigate directories
- Inspect and manipulate files
- Use
os,pathlib, andshutileffectively - Work with environment variables
- Apply safe, cross‑platform filesystem practices
These skills form the foundation for automation, data processing, deployment scripts, and any program that interacts with the real world.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Standard Library Deep Dive
Next
Datetime & Time
-
Purpose of This Lesson
-
Working with the
osModule- Getting the Current Working Directory
- Changing Directories
- Listing Directory Contents
- Creating and Removing Directories
- Checking File/Directory Existence
-
Modern Filesystem Handling with
pathlib- Creating Path Objects
- Joining Paths
- Checking Existence
- Reading and Writing Files
- Iterating Through Directories
- Creating Directories
-
Absolute vs. Relative Paths
- Relative Path
- Absolute Path
-
Inspecting File Metadata
- Using
os.stat() - Using
pathlib
- Using
-
Environment Variables
- Reading Environment Variables
- Setting Environment Variables (Process‑Local)
-
Executing System Commands
- Basic Example
- Recommended:
subprocess
-
Copying, Moving, and Deleting Files with
shutil- Copying
- Moving
- Removing Directories
-
Safe Filesystem Practices
-
Summary
