A side-by-side comparison of Python's three main modules for file-system operations: os, shutil, and pathlib.
This repository contains example scripts and reference documentation showing how to perform the same file-system tasks with each module, plus the unique features of each.
os_shutil_pathlib/
├── README.md # This file
├── LICENSE
├── .gitignore
├── examples/ # Demo scripts with identical 13 operations
│ ├── README.md
│ ├── os_demo.py
│ ├── shutil_demo.py
│ └── pathlib_demo.py
└── unique_characteristics/ # Features unique to each module
├── README.md
├── os_unique.py
├── shutil_unique.py
└── pathlib_unique.py
Each demo script performs the identical sequence of 13 file-system operations inside a tempfile.TemporaryDirectory(), so you can compare them directly:
- Create a subdirectory
- Create a text file
- List directory contents
- Check if a file exists
- Get absolute path
- Get file extension
- Get file size
- Copy a file
- Rename / move a file
- Read file content
- Delete the original file
- Delete the renamed file
- Delete the empty directory
| File | Module | Style |
|---|---|---|
examples/os_demo.py |
os + os.path |
Strings + os.path.join() |
examples/shutil_demo.py |
shutil + os.path |
High-level ops (copy2, move, rmtree) |
examples/pathlib_demo.py |
pathlib |
Object-oriented Path objects |
Scripts that demonstrate capabilities only available in one module:
| File | Unique features |
|---|---|
unique_characteristics/os_unique.py |
Environment variables, process management, file descriptors (low-level I/O), OS path constants, permissions (chmod), symlinks |
unique_characteristics/shutil_unique.py |
Copy with metadata (copy2), recursive copy/delete (copytree, rmtree), cross-filesystem move (move), disk usage (disk_usage), executable lookup (which), archive operations (make_archive, unpack_archive) |
unique_characteristics/pathlib_unique.py |
Paths as objects (/ operator), pure vs concrete paths, one-call read/write (read_text/write_text), glob pattern search (glob/rglob), path decomposition (.suffix, .stem, .name), resolve(), expanduser(), touch(), samefile() |
| Use case | Recommended module |
|---|---|
| Environment variables, processes, low-level I/O, permissions | os |
| Copy files with metadata, delete non-empty directories, move across filesystems, archives, disk usage | shutil |
| Readable path manipulation, glob search, one-call read/write, modern Python code | pathlib |
| Most general file tasks | pathlib (Python's current recommendation) |
From the repository root:
python examples/os_demo.py
python examples/shutil_demo.py
python examples/pathlib_demo.pypython unique_characteristics/os_unique.py
python unique_characteristics/shutil_unique.py
python unique_characteristics/pathlib_unique.pyEach script uses tempfile.TemporaryDirectory() internally, so nothing is left behind in the repository.
examples/README.md— Details about the demo scriptsunique_characteristics/README.md— Details about the unique feature scripts