11.1 Using pathlib to work with filenames
Most operating systems use a hierarchical tree of directories that contain files. The path from the root directory to a specific file is often shown as a string. Here’s an example path:
/Users/slott/Documents/Writing/Python Cookbook/src/ch11/recipe_01.py
This full path name lists seven named directories contained in the (unnamed) root directory. The final name has a stem of recipe_01 and a suffix of .py.
We can represent this as a string, and parse the string to locate directory names, file stems, and suffix strings. Doing this isn’t portable between the macOS and Linux operating systems, which use "/" for a separator, and Windows, which uses "\" for a separator. Further, Windows files may also have device names as a prefix to the path.
Dealing with edge cases like "/" in a filename or "." in a directory name can make string processing needlessly difficult...