eolas/neuron/14b2609c-4c92-4980-96d4-543ba5a9d0be/Constructing paths in Python.md
2025-03-27 17:52:56 +00:00

1.1 KiB

tags created
python
Sunday, March 16, 2025

Constructing paths in Python

Rather than hard-coding paths it's better to use path construction operators.

In the past this was done with os.path but a more modern library is pathlib.

import os
import pathlib
from pathlib import Path

I'll show both, side by side.

Get directory of the current script

SCRIPT_DIR = Path(__file__).parent.absolute()
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file))

Get the user home directory

HOME_DIR = Path.home
HOME_DIR = os.environ.get("HOME")

Construct a path from $HOME

HOME_DIR = Path.home
HOME_DIR = os.environ.get("HOME")

SUB_DIR = HOME_DIR / "repos" / "systems-obscure" / "src"
SUB_DIR = os.path.join(HOME_DIR, "repos", "systems-obscure", "src")

Access root directories

It's less straightforward to access directories that are not children of $HOME.

For instance to access mounted drives:

EXT_HD = Path("/media/samsung-T3")
EXT_HD_DIR = EXT_HD / "music"

EXT_HD = "/media/samsung-T3"
EXT_HD_DIR  = os.path.join(EXT_HD, "music")