The pathlib module

A more recent addition to the Python standard library is the pathlib module, a more object-oriented and somewhat higher-level take on filesystem paths, which you may remember from Chapter 7, Navigating Records with Treeview. Unlike os.path, which is just a collection of functions and constants, pathlib offers the Path object, which represents a filesystem location.

We typically use pathlib by importing the Path class from it, as follows:

>>> from pathlib import Path
>>> p = Path()
>>> print(p)
.
>>> print(p.absolute())
'/home/alanm'

Path defaults to the current working directory, but you can also provide it with an absolute or relative path string. Relative paths will be calculated against the current working directory. ...

Get Python GUI Programming with Tkinter 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.