Python Tutorial: OS Module - Use Underlying Operating System Functionality
Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Use os.getcwd() and os.chdir(path) to read and change the current working directory before running other filesystem operations.
Briefing
Python’s OS module gives direct, scriptable control over the operating system—letting code navigate the filesystem, create and delete folders, rename files, inspect file metadata, traverse directory trees, and read environment variables. That combination matters because many real-world Python apps (from web back ends to automation scripts) need to work with files and paths reliably, not just process data in memory.
The walkthrough starts with importing the built-in os module and using dir(os) to list available attributes and methods. It then demonstrates basic filesystem navigation: os.getcwd() reports the current working directory, while os.chdir(path) switches the working directory to a new location. After changing directories, os.listdir() lists the contents of the current folder (or of a specified path), which is the foundation for most file-management workflows.
Next comes directory creation and deletion. For a single-level folder, os.mkdir() creates a directory, while os.makedirs() can build a whole directory tree by creating intermediate parent folders as needed. The tutorial contrasts the two by showing how creating a nested structure fails without the intermediate directories, then succeeds when os.makedirs() is used. Deletion mirrors this distinction: os.rmdir() removes only an empty directory, whereas os.removedirs() can remove a directory tree recursively—useful when the entire demo folder structure should be removed.
File and metadata operations follow. os.rename(old_name, new_name) renames files or folders by taking the original path first and the new path second. To inspect file details, os.stat(path) returns a bundle of metadata; the tutorial highlights two practical fields: file size and modification time (mtime). Because mtime is returned as a timestamp, it’s converted into a human-readable date using datetime.fromtimestamp(). This is especially relevant when an application needs to track when files were created or updated.
For scanning larger structures, os.walk() traverses a directory tree top-down and yields three values per directory: the directory path, subdirectory names, and filenames. That makes it straightforward to search for a file you can’t locate manually, or to collect file information across an entire folder hierarchy.
Finally, the tutorial shows how to work with environment variables and paths safely. os.environ retrieves environment variables, including the home directory location. To avoid brittle string concatenation of paths, it uses os.path.join(home_path, filename) to assemble correct filesystem paths with the right separators. It also introduces common os.path utilities: os.path.basename() and os.path.dirname() for extracting filename and directory components, os.path.exists() for checking whether a path is present, os.path.isdir() and os.path.isfile() for distinguishing directories from files, and os.path.splitext() for separating a filename root from its extension. The result is a practical toolkit for building robust filesystem-aware Python programs.
Cornell Notes
The OS module provides Python programs with practical access to the underlying operating system: changing directories, listing contents, creating and deleting folders, renaming files, reading file metadata, traversing directory trees, and reading environment variables. The tutorial emphasizes safe, reliable filesystem work using os.path utilities—especially os.path.join—to avoid errors from incorrect slash placement. It also shows how to interpret os.stat() output by extracting file size and mtime, then converting timestamps into readable dates with datetime.fromtimestamp(). For large directory structures, os.walk() offers a generator-based traversal that yields directory paths, subfolders, and files at each level. Together, these tools support automation and web-app file management tasks that depend on accurate filesystem state.
How does a script determine and change its current working directory?
What’s the practical difference between os.mkdir(), os.makedirs(), and os.makedirs()’s recursive behavior?
When should a script use os.rmdir() versus os.removedirs()?
How can Python retrieve file size and modification time in a usable way?
Why is os.path.join() preferred over manually concatenating path strings?
How does os.walk() help when the location of a file is unknown?
Review Questions
- When you rename a file with os.rename(), what order do you pass the original and new names in?
- What two steps turn os.stat()’s mtime into a human-readable modification date?
- What does os.walk() yield for each directory, and how would you use those values to list all files under a root folder?
Key Points
- 1
Use os.getcwd() and os.chdir(path) to read and change the current working directory before running other filesystem operations.
- 2
Rely on os.listdir() to list files and folders in the current directory (or in a specified path).
- 3
Use os.mkdir() for single-level directory creation and os.makedirs() when parent directories may not exist.
- 4
Prefer os.rmdir() for empty directories and reserve os.removedirs() for recursive deletion when removing an entire directory tree.
- 5
Use os.rename(old, new) to rename files or folders, passing the original path first and the destination path second.
- 6
Extract file metadata with os.stat(path), then convert os.stat().st_mtime using datetime.fromtimestamp() for readable timestamps.
- 7
Build filesystem paths safely with os.path.join() and use os.path utilities (exists, isfile, isdir, basename, dirname, splitext) to validate and parse paths.