Real Python
Real Python

@realpython

4 Tweets 5 reads Jan 28, 2023
🐍⚙️ Python Tricks - Listing all the png files inside a directory
The pathlib module was first introduced in Python 3.4, containing different methods to deal with files and directories.
.rglob() helps you to recursively list files and directories that match the pattern.
Conditional Listing Using .glob()
To list files without recursion, we can make use of the .glob()
If you can spot the difference here, items inside the realpython directory are not listed.
Both .rglob() and .glob() functions use the glob pattern.
A glob pattern represents a collection of paths. Glob patterns make use of wildcard characters to match certain criteria.
For example, the single asterisk * matches everything in the directory.
With these patterns, you can flexibly match many different types of files.
To learn more about how to work with files and directories with the pathlib module, visit realpython.com.

Loading suggestions...