DEV Community

Christian Barra
Christian Barra

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

3 1

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.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs