liveBook · Manning

Pathlib

As explained in Build Python Web Apps with Streamlit

pathlib is Python's standard library module for object-oriented filesystem path manipulation.

pathlib provides Python objects for building filesystem paths without hardcoding machine-specific locations. Resolve the running script, select its directory, and construct a nearby data-file path. section 6.2.2

A portable data-loading pattern uses:

  • Path(__file__) to identify the current Python script. section 6.2.2
  • .resolve().parent to obtain the script's absolute containing directory. section 6.2.2
  • The / operator to join that directory with sales_data.csv; Path handles separators across operating systems. section 6.2.2
  • Passing the resulting Path object directly to pandas.read_csv. section 6.2.2
Listing 6.1 Loading data using data_loader.py
import pandas as pd
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent
SALES_DATA_PATH = BASE_DIR / "sales_data.csv"

def load_data():
  return pd.read_csv(SALES_DATA_PATH)

A complete loader defines BASE_DIR from the executing file, constructs SALES_DATA_PATH, and reads the CSV through pandas. section 6.2.2

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains Path construction, resolution, directory selection, joining, and pandas handoff through a complete loader.
Examples
Uses data_loader.py, sales_data.csv, BASE_DIR, SALES_DATA_PATH, and pandas.read_csv.
What it assumes
Assumes a Python script, a nearby CSV file, and basic pandas data loading.
What you can do after
Build deployment-portable CSV paths and pass them directly to pandas.read_csv.
How it connects
Connects filesystem paths with __file__, pandas, CSV files, and Streamlit data loading.

Also explained in

Compare how 2 books explain pathlib →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.