Using Python's pathlib Module (Overview)
Have you struggled with file path handling in Python? With the pathlib
module, the struggle is now over! You no longer need to scratch your head over code like this:
>>> path.rsplit('\\', maxsplit=1)[0]
And you don’t have to cringe at the verbosity of something like this:
>>> os.path.isfile(os.path.join(os.path.expanduser('~'), 'realpython.txt'))
In this video course, you’ll learn how to:
- Work with file paths in Python
- Read and write files in new ways
- Manipulate paths and the underlying file system
- List files and iterate over them
Using the pathlib
module, the two examples above can be rewritten using elegant, readable, and Pythonic code:
>>> path.parent
>>> (pathlib.Path.home() / 'realpython.txt').is_file()
That’s what you’ll master in this video course!
00:00
Using Python’s pathlib
Module. Have you struggled with file path handling in Python? With the pathlib
module, that struggle is over.
00:09 You no longer need to scratch your head over code like this or cringe at the verbosity of code such as this.
00:21
Using the pathlib
module, these two pieces of code can be rewritten using elegant, readable, and Pythonic code, as seen on-screen.
00:35
The pathlib
module allows cross-platform, object-oriented file path handling, and in this course, you’ll see how to work with file paths, the names of directories and files; learn new ways to read and write files; manipulate paths and the underlying file system; and see some examples of how to list files that iterate over them.
00:56 In this course, you’ll see two different Python REPLs being used, both of which use color-coding, which makes the syntax of examples easier to understand.
01:05
For examples that were run on macOS, bpython is being used, while on Windows, the IPython terminal is being used. However, all the code you see running is standard Python and could be run in the Python REPL, which is typically accessed by typing python
at your terminal or command prompt.
01:26 So now you know what’s going to be covered, let’s get started.
Become a Member to join the conversation.