Idiomatic Python pathlib vs. os.path

Related: Require minimum Python version: setup.py


pathlib is an object-oriented, Python standard library way to handle paths and filenames vs. “dumb” strings. pathlib.Path() replaces most cumbersome code from os.path and glob.glob.

pathlib is standard library and supported throughout Python ≥ 3.6 natively, as well as in SciPy, Numpy, et al.

Example

This examples eliminates messy os.path.join():

filename = pathlib.Path('mydir/a/b') / 'myfile'

Benchmark

pathlib is very efficient, for example with Python 3.6.2 and Ipython 6.1.0:

%timeit Path('~')
2.46 µs ± 24.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit Path('~').expanduser()
7.35 µs ± 7.13 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Install pathlib for Python ≥ 2.6

Python standard library pathlib can be used for Python ≥ 2.6 via

python -m pip install pathlib2

import pathlib: Python 2 / 3 compatible

Use pathlib for Python ≥ 2.6 by putting in __init__.py

try:
    from pathlib import Path
    Path().expanduser()
except (ImportError, AttributeError):
    from pathlib2 import Path

In the other module .py files (same directory as __init__.py)

from . import Path

file globbing

Pathlib can glob like GNU find:

pathlib.Path.glob() recursively searches for files.

Example: search for all .py files in your ~ home directory:

allpy = pathlib.Path('~').expanduser().glob('*.py')

allpy is a generator that you can either

  • iterate over with a for loop
  • enclose in sorted() to get a list. flist = sorted(allpy)

Reference