Pathlib

Pathlib is a standard-library component that improves python's cross-platform compatability.

  • Different Operating Systems have different ways of describing filesystem paths
  • Pathlib abstracts these differences away, allowing for code that behaves the same way regardless of OS.

For example:

  • On Windows, the "My Documents" folder exists at C:/users/{user}/Documents
  • On Linux, the equivalent path would be ~/Documents which typically expands to /home/{user}/Documents.
  • With pathlib, we can refer to this platform-specific directory using pathlib.Path.home:
from pathlib import Path

# Path.home refers to the user's home directory
documents = Path.home() / "Documents"

Note that directories and files are accessible by "dividing" an existing pathlib.Path object.

  • By doing it this way, filesystem and os-specific path quirks are abstracted away.

Here is a more complete example, which will be built-upon in the following chapters:

# uci_bootcamp_2021/examples/pathlib_example.py

# Brings the module `pathlib` into scope.
import pathlib


# define a function called `main`, which contains the example code.
def main():
    # Create a pythonic pointer to the data file ./data/us-graph-2015-02.csv
    # <repository_root>/data/us-graph-2015-02.csv
    target = pathlib.Path() / "data" / "us-graph-2015-02.csv"

    # read the lines out of the data file
    lines = target.read_text().splitlines()
    # emit the first line of the data
    print(lines[0])

This example creates a Path to data/us-graph-2015-02.csv, which is one of the example datasets provided in this presentation's repository.

Path objects have many methods on them, including read_text, which can be used to read the text out of the file without having to explicitly open it.