Discovering the pathlib module

barrachri profile image Christian Barra Updated on ・1 min read

The Python Standard Library is like a gold mine, and the pathlib module is really a gem.

pathlib provides an useful abstraction on top of different filesystems (posix and Windows).

But that's just a small part of the very friendly user experience that you can have.

# files in my folder
README.md  example.py subfolder/README.md

We can import Path from pathlib

from pathlib import Path

path = Path(".")
# PosixPath('.')

files = [file for file in path.iterdir()]
# [PosixPath('subfolder'), PosixPath('README.md'), PosixPath('example.py')]

Path() returns (in this case) a pathlib.PosixPath object with a very handy iterdir, a useful generator for when you have a lot of files in your folder.

<generator object Path.iterdir at 0x10aca2cf0>

Do you want to get only the files with a specific format?

md_files = [
    file for file in path.iterdir() if file.suffix == '.md'
# [PosixPath('README.md')]

You can get a similar result using glob

md_files = [file for file in path.glob("*.md")]
# [PosixPath('README.md')]

glob is quite powerful, using the ** pattern you can search recursively

md_files = [file for file in path.glob("**/*.md")]
# [PosixPath('README.md'), PosixPath('subfolder/README.md')]

If you want to learn more about the pathlib module the PEP 428 and the official documentation are the right places where to start.

Posted on by:

barrachri profile

Christian Barra


Technical Lead at INFARM • Trainer • Founder • @ThePSF Fellow


Editor guide