By WebNest Studio Engineering Team
Python Tutorial
The os, sys and pathlib Modules
Scripts constantly interact with their environment: reading environment variables, listing and creating folders, building file paths that work on Windows and Linux, checking the Python version, or exiting with an error code. The os, sys and pathlib modules handle all of this, and shutil adds high-level copying and moving.
This lesson covers each module's most useful features, with a strong recommendation to use pathlib for paths in new code.
Why it matters
Scripts that organise files, read configuration from environment variables or process every file in a folder all depend on these modules. Building paths by joining strings with slashes breaks on a different operating system; pathlib handles the differences and is the standard in current Python code.
pathlib: Object-Oriented Paths
Path objects represent file-system paths. Join them with /, read parts with .name, .stem, .suffix, .parent, check with .exists(), .is_file(), .is_dir(), create with .mkdir(parents=True, exist_ok=True), read and write with .read_text()/.write_text(), and search with .glob("*.csv") and .rglob(). Paths automatically use the right separator for the operating system.
The os Module
os.environ and os.getenv("KEY", default) read environment variables (the standard place for configuration and secrets). os.getcwd(), os.chdir(), os.listdir(), os.makedirs(), os.remove(), os.rename() and os.walk() work with the file system; os.path contains older path helpers; os.cpu_count() and os.getpid() give system information.
The sys Module
sys.argv holds command-line arguments; sys.exit(code) ends the program with an exit status; sys.version and sys.version_info identify the interpreter; sys.platform the operating system; sys.path the module search path; sys.stdin, sys.stdout and sys.stderr the standard streams; sys.getsizeof() an object's memory size.
shutil
shutil.copy(), copytree(), move(), rmtree() (delete a folder and everything in it — carefully!), make_archive() for zip files, and disk_usage().
Examples
Working with paths using pathlib
from pathlib import Path
base = Path("demo_project")
(base / "data").mkdir(parents=True, exist_ok=True)
(base / "data" / "students.csv").write_text("name,marks\nAsha,91\n")
(base / "data" / "notes.txt").write_text("hello")
(base / "README.md").write_text("# Demo")
report = base / "data" / "students.csv"
print(report.name, report.stem, report.suffix, report.parent.name)
print(report.exists(), report.is_file(), (base / "data").is_dir())
print(report.read_text().splitlines())
print(sorted(p.name for p in base.rglob("*.*")))
print(sorted(p.name for p in (base / "data").glob("*.csv")))
students.csv students .csv data
True True True
['name,marks', 'Asha,91']
['README.md', 'notes.txt', 'students.csv']
['students.csv']
Environment variables and directory walking with os
import os
os.environ["APP_MODE"] = "development"
print(os.getenv("APP_MODE"), os.getenv("DB_PASSWORD", "not set"))
os.makedirs("walk_demo/a/b", exist_ok=True)
for name in ["walk_demo/top.txt", "walk_demo/a/mid.txt", "walk_demo/a/b/deep.txt"]:
with open(name, "w") as f:
f.write("x")
for folder, subfolders, files in sorted(os.walk("walk_demo")):
print(folder.replace(os.sep, "/"), sorted(subfolders), files)
print(os.path.join("data", "2026", "report.csv").replace(os.sep, "/"))
print(os.path.splitext("photo.jpeg"), os.path.basename("/tmp/a/b.txt"))
development not set
walk_demo ['a'] ['top.txt']
walk_demo/a ['b'] ['mid.txt']
walk_demo/a/b [] ['deep.txt']
data/2026/report.csv
('photo', '.jpeg') b.txt
The sys module
import sys
print(sys.version_info >= (3, 8), type(sys.argv).__name__)
print(sys.getsizeof([]) < sys.getsizeof([1, 2, 3]))
print("stdout and stderr:", sys.stdout is not None, sys.stderr is not None)
print("errors are written to", sys.stderr.name)
def main():
if len(sys.argv) > 5:
sys.exit("too many arguments")
print("argv ok")
main()
True list
True
stdout and stderr: True True
errors are written to <stderr>
argv ok
Copying, moving, archiving and deleting with shutil
import shutil
from pathlib import Path
src = Path("shutil_demo/src")
src.mkdir(parents=True, exist_ok=True)
(src / "a.txt").write_text("A")
shutil.copy(src / "a.txt", src / "a_copy.txt")
shutil.copytree(src, "shutil_demo/backup", dirs_exist_ok=True)
shutil.move("shutil_demo/src/a_copy.txt", "shutil_demo/moved.txt")
archive = shutil.make_archive("shutil_demo/backup_zip", "zip", "shutil_demo/backup")
print(sorted(p.name for p in Path("shutil_demo").iterdir()))
print(Path(archive).suffix)
shutil.rmtree("shutil_demo")
print(Path("shutil_demo").exists())
['backup', 'backup_zip.zip', 'moved.txt', 'src']
.zip
False
Common Mistakes
- Building paths with string concatenation and hard-coded "\\" or "/" separators.
- Hard-coding secrets instead of reading them from environment variables.
- Using shutil.rmtree on a path built from user input without checks.
- Assuming the current working directory is the script's folder; use Path(__file__).parent.
Exercise
Using pathlib, print the file name of the path, its extension, the name of the folder that contains it, and the name the file would have with a .json extension. PurePosixPath is used so that the result is the same on every operating system.
Expected output: sales.csv, .csv, reports, sales.json (one per line)
from pathlib import PurePosixPath
path = PurePosixPath("/home/asha/reports/sales.csv")
# TODO: print the file name
# TODO: print the extension
# TODO: print the name of the parent folder
# TODO: print the file name with the extension changed to .json
Show hints
- The attributes are
name,suffixandparent. path.with_suffix(".json")returns a new path; take itsname.
Show solution
from pathlib import PurePosixPath
path = PurePosixPath("/home/asha/reports/sales.csv")
print(path.name)
print(path.suffix)
print(path.parent.name)
print(path.with_suffix(".json").name)
Quick quiz
1. How are two parts of a path joined with pathlib?
- A. With the + operator
- B. With a comma
- C. With the / operator
- D. With join()
Show answer
C. For example, Path("data") / "report.csv".
2. What does os.environ.get("API_KEY") return when the variable is not set?
- A. It raises KeyError
- B. None
- C. An empty string
- D. False
Show answer
B. os.environ["API_KEY"] would raise KeyError. get() can also take a default.
3. What is in sys.argv[0]?
- A. The first argument given by the user
- B. The current folder
- C. The Python version
- D. The name of the script being run
Show answer
D. The user's arguments start at index 1.
Interview questions
Why is pathlib preferred to os.path?
Show answer
os.path is a set of functions that work on strings, so code becomes nested calls such as os.path.join(os.path.dirname(p), "x"). pathlib represents a path as an object with attributes and methods: p.parent / "x", p.suffix, p.exists(), p.read_text(). It is easier to read, handles the separators of each operating system, and is accepted by the functions of the standard library.
How should a program read secrets such as API keys?
Show answer
From environment variables, not from the source code, so they are not committed to version control and can differ between machines. os.environ.get("NAME") reads one and returns None if it is missing; the program should check for that and stop with a clear message. In development the variables are often loaded from a .env file that is excluded from version control.
Key Points to Remember
- pathlib.Path: join with /, inspect name/stem/suffix/parent, read/write text, glob files.
- os: environment variables, directories, walking trees, system info.
- sys: argv, exit, version, platform, path and standard streams.
- shutil: copy, copytree, move, rmtree, make_archive.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering