Discovering the pathlib module

December 11, 2018

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 subfolder/

We can import Path from pathlib

from pathlib import Path

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

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

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('')]

You can get a similar result using glob

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

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

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

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

Enjoyed this post? For more, follow me on twitter.

CC BY-SA 4.0