Chapter 6. The File System

Python’s standard library includes a large range of tools for working with files on the file system, building and parsing filenames, and examining file contents.

The first step in working with files is to determine the name of the file to work on. Python represents filenames as simple strings, but provides tools for building them from standard, platform-independent components in os.path (page 296).

The pathlib (page 305) module provides an object-oriented API for working with file system paths. Using it instead of os.path offers greater convenience because it operates at a higher level of abstraction.

List the contents of a directory with listdir() from os (page 1227), or use glob (page 319) to build a list of filenames ...

Get The Python 3 Standard Library by Example, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.