On this page

OS & Filesystem

Enrico Hochkoeppler Profile

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:

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:

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​

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:

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!

Buy Me a Coffee

Previous

Standard Library Deep Dive

Next

Datetime & Time