Idiomatic Python 2/3 pathlib vs. os.path

Related: Require minimum Python version:

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 Python supported throughout Python natively, as well as in SciPy, Numpy, et al.


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

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


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: Python ≤ 3.4

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

pip install pathlib2

Import pathlib: Python 2/3 compatible

Use pathlib for Python ≥ 2.6 by

  1. put in
        from pathlib import Path
    except (ImportError,AttributeError):
        from pathlib2 import Path
  2. in the other module .py files (same directory as
    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)




Leave a Comment