Object-oriented filesystem paths
Attention: this backport module isn’t maintained anymore. If you want to report issues or contribute patches, please consider the pathlib2 project instead.
pathlib offers a set of classes to handle filesystem paths. It offers the following advantages over using string objects:
Python 3.2 or later is recommended, but pathlib is also usable with Python 2.7 and 2.6.
In Python 3.4, pathlib is now part of the standard library. For Python 3.3 and earlier, easy_install pathlib or pip install pathlib should do the trick.
Importing the module classes:
>>> from pathlib import *
Listing Python source files in a directory:
>>> list(p.glob('*.py')) [PosixPath('test_pathlib.py'), PosixPath('setup.py'), PosixPath('pathlib.py')]
Navigating inside a directory tree:
>>> p = Path('/etc') >>> q = p / 'init.d' / 'reboot' >>> q PosixPath('/etc/init.d/reboot') >>> q.resolve() PosixPath('/etc/rc.d/init.d/halt')
Querying path properties:
>>> q.exists() True >>> q.is_dir() False
Opening a file:
>>> with q.open() as f: f.readline() ... '#!/bin/bash\n'
The full documentation can be read at Read the Docs.
However, if you find an issue specific to prior versions of Python (such as 2.7 or 3.2), you can post an issue on the BitBucket project page.
This version brings pathlib up to date with the official Python 3.4 release, and also fixes a couple of 2.7-specific issues.
The API in this version is partially incompatible with pathlib 0.8 and earlier. Be sure to check your code for possible breakage!