DEV Community

Christian Barra
Christian Barra

Posted on • Edited on • Originally published at christianbarra.com

Discovering the pathlib module

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
Enter fullscreen mode Exit fullscreen mode

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')]
Enter fullscreen mode Exit fullscreen mode

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.

path.iterdir()
<generator object Path.iterdir at 0x10aca2cf0>
Enter fullscreen mode Exit fullscreen mode

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')]
Enter fullscreen mode Exit fullscreen mode

You can get a similar result using glob

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

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')]
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)