Learn os

Portable access to the operating system: files, directories, env and processes

13 lessons · 2 hours · 4 levels · covers os Python 3.13

The os module is Python's standard interface to the operating system: the environment, the current directory, files and directories, permissions, file descriptors and processes. It works the same way on Linux, macOS and Windows where it can, and exposes the platform-specific calls where it can't. Every script that touches the file system or its environment ends up using it.

Before you start

os builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 32 min in total
2. PracticalUse it productively at work, with the usual idioms7 lessons · 1 hour in total
3. AdvancedThe less common features, performance and trade-offs10 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know13 lessons · 2 hours in total

The os tree

osThe os module and the working directoryThe os module andthe working directoryEnvironment variablesEnvironmentvariablesBuilding paths with os.pathBuilding paths withos.pathListing directoriesListing directoriesCreating, renaming and deletingCreating, renamingand deletingWalking a directory treeWalking a directorytreeFast listings with os.scandirFast listings withos.scandirModules and importsModules and importsDictionariesDictionariesStrings and f-stringsStrings and f-stringsIterators and generatorsIterators andgeneratorsContext managers and withContext managersand with
Essentials and Practical shown (7 of 13 lessons); the full list is below. Click a lesson to read it.

All 13 lessons, in order

Basics

  1. The os module and the working directory · Essentials · 7 min

    What the os module is for, how it stays portable, and how the current working directory affects relative paths.

  2. Environment variables · Essentials · 8 min

    Read, set and remove environment variables with os.environ and os.getenv, and understand what child processes see.

Paths and directories

  1. Building paths with os.path · Essentials · 10 min

    Join, split and normalise path strings portably with os.path, and test whether paths exist.

  2. Listing directories · Essentials · 7 min

    List what a directory contains with os.listdir and filter the entries into files and subdirectories.

  3. Creating, renaming and deleting · Practical · 9 min

    Create directory trees, rename or move entries, and delete files and folders safely.

  4. Walking a directory tree · Practical · 10 min

    Traverse a whole tree with os.walk, prune subdirectories in place, and choose top-down or bottom-up order.

Files and metadata

  1. Fast listings with os.scandir · Practical · 9 min

    Iterate directory entries as DirEntry objects that cache type and stat information.

  2. File metadata and permissions · Advanced · 12 min

    Read sizes, timestamps and mode bits with os.stat, and change permissions with os.chmod.

  3. Atomic file updates · Advanced · 11 min

    Replace a file safely with a temporary file, flush and fsync, then os.replace.

Processes

  1. Process information and resources · Advanced · 9 min

    Inspect the running process with os.getpid, os.cpu_count, os.process_cpu_count and friends, and know when to use subprocess instead of os.system.

  2. fork, exec and wait · Expert · 14 min

    Understand the Unix process model behind subprocess and multiprocessing: os.fork, the exec family and os.waitpid.

Low-level interfaces

  1. File descriptors and os.open · Expert · 14 min

    Work below the file object level with integer file descriptors, open flags, os.read/os.write and pipes.

  2. Bytes paths and filesystem encoding · Expert · 12 min

    How Python maps file names to bytes, why surrogateescape exists, and how os.fspath and os.PathLike tie paths together.