Picking Out Components of a Path
If you’d like a visual representation of these and other properties and methods, then check out Pathlib Cheatsheet.
00:00
Picking Out Components of a Path. One of the most convenient features of Path
is that different parts are available as properties. Basic examples include .name
, the file name without any directory; .parent
, the directory containing the file, or the parent directory if the path is a directory; .stem
, the filename without the suffix; .suffix
, the file extension; and .anchor
, the part of the path before the directories.
00:32 On-screen, you’ll see these properties in action.
00:58
Note that .parent
returns a new path object, whereas the other properties return strings. This means that .parent
can be chained or combined with /
to create completely new paths.
01:22
The excellent Pathlib Cheatsheet provides a visual representation of these and other properties and methods. In the next section of the course, you’ll take a look at some of the finer details of the Path
class.
Become a Member to join the conversation.