pathlib /

Filename Size Date modified Message
56 B
Now all tests pass
180 B
Added tag 0.8 for changeset 8cea6493e8cc
1.1 KB + license
109 B
Make more fine-grained
3.1 KB
README.txt edited online with Bitbucket
4 B
48.4 KB
Issue #12: is_dir and friends return False when path doesn't exist or is a broken symlink, instead of raising OSError.
977 B
Add classifier for Python 3.3
60.4 KB
Add broken symlink tests

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:

  • No more cumbersome use of os and os.path functions. Everything can be done easily through operators, attribute accesses, and method calls.
  • Embodies the semantics of different path types. For example, comparing Windows paths ignores casing.
  • Well-defined semantics, eliminating any warts or ambiguities (forward vs. backward slashes, etc.).


Python 3.2 or later is recommended, but pathlib is also usable with Python 2.7.


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:

>>> p = Path('.')
>>> list(p.glob('*.py'))
[PosixPath(''), PosixPath(''),

Listing all Python source files in this directory tree:

>>> list(p.glob('**/*.py'))
[PosixPath(''), PosixPath(''),
 PosixPath(''), PosixPath('docs/'),

Listing subdirectories:

>>> [x for x in p if x.is_dir()]
[PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),
 PosixPath('__pycache__'), PosixPath('build')]

Navigating inside a directory tree:

>>> p = Path('/etc')
>>> q = p['init.d/reboot']
>>> q
>>> q.resolve()

Querying path properties:

>>> q.exists()
>>> q.is_dir()
>>> q.st_mode

Opening a file:

>>> with as f: f.readline()


The full documentation can be read at Read the Docs.


The issue tracker and repository are hosted by BitBucket.


Version 0.8

  • Add and PurePath.anchor.
  • Add Path.owner and
  • Add Path.replace().
  • Add Path.as_uri().
  • Issue #10: when creating a file with, don't set the executable bit.
  • Issue #11: fix comparisons with non-Path objects.

Version 0.7

  • Add '**' (recursive) patterns to Path.glob().
  • Fix openat() support after the API refactoring in Python 3.3 beta1.
  • Add a target_is_directory argument to Path.symlink_to()

Version 0.6

  • Add Path.is_file() and Path.is_symlink()
  • Add Path.glob() and Path.rglob()
  • Add PurePath.match()

Version 0.5

  • Add Path.mkdir().
  • Add Python 2.7 compatibility by Michele Lacchia.
  • Make parent() raise ValueError when the level is greater than the path length.