Idiomatic Python pathlib

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

Eliminate 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)

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

Example: search for all .py files under 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)