📝 Edit on GitHub
Pathlib
New builtin alternative to
os.path
Resources
- pathlib in Python 3 docs.
- pathlib intro on PBPython
- Pathlib on RealPython
Basics
Import
from pathlib import Path
List subdirectories
p = Path('.')
dir_paths = [x for x in p.iterdir() if x.is_dir()]
Glob
list(p.glob('**/*.py'))
Navigating
p = Path('/etc')
q = p / 'init.d' / 'reboot'
# => PosixPath('/etc/init.d/reboot')
q.resolve()
# => PosixPath('/etc/rc.d/init.d/halt')
Querying path properties:
q.exists()
q.is_dir()
Opening a file
with q.open() as f_in:
text = f_in.readline()
Real projects
Absolute path to app directory
If it dangerous to just use Path('.')
as that is the current working directory, not the directory of the script.
The approach is useful when running the script from anywhere in or outside the repo and keeping paths relative to that app directory i.e. the top-level module inside the repo.
my_repo/
my_app/
my_app.py
constants.py
After research, I found this to be effective. It resolves relative and symlinks (I think?) and the directory will get parent directory.
APP_DIR = Path(__file__).resolve().parent
This should be set in the config or constants module, so it can be imported by other scripts.
from constants import APP_DIR
Path to var directory
For writing CSV or log files, for example.
VAR_DIR = APP_DIR / "var"
APP_LOG_PATH = VAR_DIR / "app.log"