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 Directory
    • Path.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

os and os.pathpathlib
os.path.abspath()Path.resolve()
os.chmod()Path.chmod()
os.mkdir()Path.mkdir()
os.makedirs()Path.mkdir(parents=True)
os.rename()Path.rename()
os.replace()Path.replace()
os.rmdir()Path.rmdir()
os.remove(), os.unlink()Path.unlink()
os.getcwd()Path.cwd()
os.path.exists()Path.exists()
os.path.expanduser()Path.expanduser() and Path.home()
os.listdir()Path.iterdir()
os.path.isdir()Path.is_dir()
os.path.isfile()Path.is_file()
os.path.islink()Path.is_symlink()
os.link()Path.link_to()
os.symlink()Path.symlink_to()
os.readlink()Path.readlink()
os.stat()Path.stat(), Path.owner(), Path.group()
os.path.isabs()PurePath.is_absolute()
os.path.join()PurePath.joinpath()
os.path.basename()PurePath.name
os.path.dirname()PurePath.parent
os.path.samefile()Path.samefile()
os.path.splitext()PurePath.suffix

pathlib cheatsheet

pathlib cheatsheet

Google Colab Notebook

Open in Colab

Reference