pathlib
Why patblib
?
The pathlib
module, introduced in Python 3.4 (PEP 428), gathers the necessary functionality in one place and makes it available through methods and properties on an easy-to-use Path
object.
from pathlib import Path
Createing paths
Use class methods:
Path.cwd()
: get Current Working DirectoryPath.home()
: get home directory
Create from string representation explicitly
E.g.
Path("/content/src/demo.py")
Construct path by joining parts of the path
E.g.
Path.joinpath(Path.cwd(), "src", "demo.py")
Reading and writing files
The built-in open()
function can use Path
objects directly. E.g.:
with open(path, mode="r") as f:
pass
For simple reading and writing of files, there are a couple of convenience methods in the pathlib
library:
.read_text()
: open the path in text mode and return the contents as a string..read_bytes()
: open the path in binary/bytes mode and return the contents as a bytestring..write_text()
: open the path and write string data to it..write_bytes()
: open the path in binary/bytes mode and write data to it.
Each of these methods handles the opening and closing of the file, making them trivial to use.
Components of path
The different parts of a path are conveniently available as properties. Basic examples include:
.name
: the file name without any directory.parent
: the directory containing the file, or the parent directory if path is a directory.stem
: the file name without the suffix.suffix
: the file extension.anchor
: the part of the path before the directories
path = Path.joinpath(Path.cwd(), "src", "hello-world.txt")
print(f"Full path: {path}")
print(f"Name: {path.name}")
print(f"Parent: {path.parent}")
print(f"Stem: {path.stem}")
print(f"Suffix: {path.suffix}")
print(f"Anchor: {path.anchor}")
Full path: /content/src/hello-world.txt
Name: hello-world.txt
Parent: /content/src
Stem: hello-world
Suffix: .txt
Anchor: /
Renaming files
When you are renaming files, useful methods might be .with_name()
and .with_suffix()
. They both return the original path but with the name or the suffix replaced, respectively.
Deleting files
Directories and files can be deleted using .rmdir()
and .unlink()
respectively.
Listing files
Path.iterdir()
: iterates over all files in the given directory- More flexible file listing
Path.glob()
Path.rglob()
(recursive glob)
Moving files
Path(current_location).rename(new_location)
Correspondence to tools in the os
module
pathlib
cheatsheet
Google Colab Notebook
Open in Colab
Reference
Documentation:
pathlib
— Object-oriented filesystem paths