Idiomatic Python 2/3 pathlib vs. os.path

1 minute read

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 for Python ≥ 3.5, 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: Python ≤ 3.4

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

  1. put in __init__.py
    try:
        from pathlib import Path
        Path().expanduser()
    except (ImportError,AttributeError):
        from pathlib2 import Path
    
  2. 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

Categories:

Updated:

Leave a comment