Get AI summaries of any video or article — Sign up free
Python Tutorial: OS Module - Use Underlying Operating System Functionality thumbnail

Python Tutorial: OS Module - Use Underlying Operating System Functionality

Corey Schafer·
4 min read

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.

TL;DR

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?

Use os.getcwd() to print the current working directory. Then use os.chdir(path) with a path string to move to a new location. After changing directories, os.getcwd() will reflect the updated location, and subsequent relative operations (like listing) will apply to that new directory.

What’s the practical difference between os.mkdir(), os.makedirs(), and os.makedirs()’s recursive behavior?

os.mkdir(name) creates only a single directory level and fails if parent directories don’t exist. os.makedirs(name) creates intermediate directories as needed, so nested structures like a top-level folder plus a subfolder can be created in one call. The tutorial demonstrates that creating a subdirectory under a non-existent parent errors with mkdir but succeeds with makedirs.

When should a script use os.rmdir() versus os.removedirs()?

os.rmdir(path) removes only an empty directory, so it won’t work if the directory contains files or subdirectories. os.removedirs(path) removes directories recursively, deleting the entire tree of folders when that’s the intended outcome. The tutorial treats recursive deletion as riskier than recursive creation and uses removedirs when the whole demo directory tree should be removed.

How can Python retrieve file size and modification time in a usable way?

Call os.stat(file_path) and read fields from the returned stat result. The tutorial highlights file size and mtime (modification time). Since mtime is a timestamp, convert it to a human-readable date using datetime.fromtimestamp(mod_time).

Why is os.path.join() preferred over manually concatenating path strings?

Manual concatenation often leads to missing or double slashes, because different paths may or may not already end with a separator. os.path.join(home_path, 'test.txt') reliably inserts the correct separator and produces a valid combined path. The tutorial shows that naive concatenation can produce an incorrect path, while os.path.join avoids that guesswork.

How does os.walk() help when the location of a file is unknown?

os.walk(start_path) traverses the directory tree top-down and yields a tuple of (dirpath, dirnames, filenames) for each directory encountered. By iterating through these yields, a script can inspect every folder and file under start_path, enabling search across an entire hierarchy and collection of file information for tasks like web-app directory indexing.

Review Questions

  1. When you rename a file with os.rename(), what order do you pass the original and new names in?
  2. What two steps turn os.stat()’s mtime into a human-readable modification date?
  3. 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. 1

    Use os.getcwd() and os.chdir(path) to read and change the current working directory before running other filesystem operations.

  2. 2

    Rely on os.listdir() to list files and folders in the current directory (or in a specified path).

  3. 3

    Use os.mkdir() for single-level directory creation and os.makedirs() when parent directories may not exist.

  4. 4

    Prefer os.rmdir() for empty directories and reserve os.removedirs() for recursive deletion when removing an entire directory tree.

  5. 5

    Use os.rename(old, new) to rename files or folders, passing the original path first and the destination path second.

  6. 6

    Extract file metadata with os.stat(path), then convert os.stat().st_mtime using datetime.fromtimestamp() for readable timestamps.

  7. 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.

Highlights

os.walk() yields (dirpath, dirnames, filenames) for each directory, making full-tree scanning straightforward.
os.stat() provides both file size and mtime, and datetime.fromtimestamp() turns mtime into a readable date.
os.path.join() prevents common path bugs caused by missing or duplicate slashes during string concatenation.
os.makedirs() can create nested directory structures in one step by generating intermediate folders.
os.rmdir() removes only empty directories, while os.removedirs() can delete directory trees recursively.

Topics

  • OS Module Basics
  • Filesystem Navigation
  • Recursive Directories
  • File Metadata
  • Path Handling