Using Python's pathlib Module (Summary)
Since Python 3.4, pathlib
has been available in the standard library. With pathlib
, file paths can be represented by proper Path
objects instead of plain strings as before. These objects make code dealing with file paths:
- Easier to read, especially because
/
is used to join paths together - More powerful, with most necessary methods and properties available directly on the object
- More consistent across operating systems, as peculiarities of the different systems are hidden by the
Path
object
In this video course, you’ve seen how to create Path
objects, read and write files, manipulate paths and the underlying file system, as well as some examples of how to iterate over many file paths.
To learn more about pathlib
, check out:
For more information on related topics, explore:
- Working With Files in Python
- How to Get a List of All Files in a Directory With Python
- Object-Oriented Programming (OOP) in Python 3
- How to Use Generators and yield in Python
- When to Use a List Comprehension in Python
- The Walrus Operator: Python 3.8 Assignment Expressions
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00
Summary. Since Python 3.4, pathlib
has been available in the standard library. With pathlib
, file paths can be represented by proper Path
objects instead of as plain strings. These objects make code dealing with file paths easier to read, particularly because /
is used to join paths together; more powerful, with most necessary methods and properties available directly on the object; and more consistent across operating systems, as peculiarities of the different systems are hidden by the Path
object.
00:35
In this course, you’ve seen how to create Path
objects, read and write files, manipulate paths and the underlying file system, as well as some examples of how to iterate over many file paths.
00:48 We hope you found this course useful and we’ll see you again soon at realpython.com.
Darren Jones RP Team on Jan. 4, 2023
@east4ming - Generally we don’t provide code for REPL sessions (as it’s not something you’d generally run in the same way you might a python script), but it’s something I’ll discuss further with the team. Thanks for reaching out.
Become a Member to join the conversation.
east4ming on Dec. 28, 2022
Need sample codes in this lesson. Thanks!