liveBook · Manning

Chapter 12: Using the filesystem

What you will learn in chapter 12 of The Quick Python Book, Fourth Edition

The introduction distinguishes filesystem work from file I/O and explains why portable file references matter when working across operating systems.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction distinguishes filesystem work from file I/O and explains why portable file references matter when working across operating systems.

Key terms: basic I/O, filesystem, filesystem conventions, cross-platform filesystem interaction

12.1 os and os.path vs. pathlib

This section contrasts the traditional os and os.path approach to filesystem operations with pathlib and explains why the book shows both.

Key terms: os, os.path, pathlib, file paths, filesystem operations

12.2 Paths and pathnames

Explains how pathnames locate files in a filesystem tree and why operating-system differences matter when working with them.

Key terms: pathname, path, filesystem tree, root, separator, drive, case, lowercase filenames

12.2.1 Absolute and relative paths

This subsection distinguishes absolute from relative pathnames and explains how a relative path gets a filesystem location from its context.

Key terms: absolute pathname, relative pathname, root of the filesystem, relative path, absolute path, current working directory, os.listdir(path)

12.2.2 The current working directory

This section explains how Python’s current working directory affects filesystem operations and how to inspect or change it with os.

Key terms: current working directory, os.getcwd, os.chdir, os.curdir, relative path, os.listdir

12.2.3 Accessing directories with pathlib

This subsection distinguishes getting the current directory with pathlib from changing the process’s current directory.

Key terms: pathlib, current directory, os.chdir(), path object

12.2.4 Manipulating pathnames

This section shows how to construct and dissect pathnames with os.path while avoiding operating-system-specific path syntax.

Key terms: os.path.join, path components, subpaths, absolute path, relative path, network resource, os.path.split, os.path.basename, os.path.dirname, os.path.splitext

12.2.5 Manipulating pathnames with pathlib

This section shows how pathlib path objects construct paths across operating systems and expose their components through properties.

Key terms: path object, slash operator, forward slashes, backslashes, parts, name, parent, suffix, basename

12.2.6 Useful constants and functions

This subsection introduces operating-system path indicators, platform identifiers, and environment variables available through Python.

Key terms: os.curdir, os.pardir, os.listdir, current working directory, relative path, os.name, sys.platform, nt, posix, win32

12.3 Getting information about files

This section introduces functions for checking what a filesystem path points to and retrieving basic information about it.

Key terms: os.path.exists, os.path.isfile, os.path.isdir, Path objects, os.path.islink, os.path.ismount, os.path.samefile, os.path.isabs, symbolic links, Windows shortcuts

12.3.1 Getting information about files with scandir

This subsection shows how os.scandir provides information about directory entries and why it should be used with a context manager.

Key terms: os.scandir, os.DirEntry, os.listdir, os.path, is_dir, is_file, context manager, with

12.4 More filesystem operations

This section introduces cross-platform commands for listing and matching files, moving and deleting files, and creating and removing directories.

Key terms: os.listdir, os.curdir, os.pardir, glob, glob module, pathname, *, ?, character sequence, os.rename

12.4.1 More filesystem operations with pathlib

This section shows how pathlib Path objects list, move, create, and remove filesystem entries, and contrasts their path-specific methods with os functions.

Key terms: Path objects, os functions, iterdir, glob, iterator of path objects, WindowsPath, PosixPath, rename, unlink, mkdir

12.5 Processing all files in a directory subtree

This section explains how to traverse a directory tree with os.walk and identifies shutil functions for copying or removing directory trees.

Key terms: os.walk, iterator, root, subdirectories, files, topdown, onerror, followlinks, shutil, copytree

12.6 More file operations

This exercise asks the reader to find qualifying .test files, total their sizes, and move them into a backup subdirectory.

Key terms: .test, symlinks, os and os.path, pathlib, .test files, backup, subdirectory

12.6.1 Solving the problem with AI-generated code

This subsection frames the filesystem exercise as an incremental solution that can be adapted between os and pathlib, including with AI-generated code.

Key terms: os, pathlib, backup directory, refactor

12.6.2 Solutions and discussion

12.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 11: Python programs Chapter 13: Reading and writing files →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.