Using pathlib to work with filenames
Most operating systems use a hierarchical path to identify a file. Here's an example filename, including the entire path:
/Users/slott/Documents/Writing/Python Cookbook/code
This full pathname has the following elements:
- The leading
/means the name is absolute. It starts from the root of the directory of files. In Windows, there can be an extra letter in front of the name, such asC:, to distinguish between the directories on individual storage devices. Linux and macOS treat all the devices as a unified hierarchy. - The names
Users,slott,Documents,Writing,Python Cookbook, andcoderepresent the directories (or "folders," as a visual metaphor) of the filesystem. The path names a top-levelUsersdirectory. This directory is expected to contain theslotsubdirectory. This is true for each name in the path. /is a separator between directory names. The Windows OS uses\to separate items on the...