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

