Find all files in a directory with extension .txt in Python
Engage glob
for a quick win:
This one-liner reveals all .txt
files in the current directory and all sub-folders. Set recursive=False
to stop going deeper than the current directory. Keep it shallow!
Diving into the nitty-gritty of directory scan
Just dabble with glob
in your current directory
To grab all .txt
files in the current directory in a flash, ask glob
politely:
The wildcard *
matches any preceding characters, making it perfect for snatching all .txt
files!
Digging deeper with os.walk
You want to unearth .txt
files in subdirectories as well? Here you go:
os.walk
takes you on a walkathon across directories, endswith
only picks up the .txt
files, and os.path.join
assembles the actual path.
When you need the real deal with full paths
When you're in the mood for serious file trickery, you need full paths:
Now, txt_files
has all your favorite absolute file paths. You can't get any more real than this!
Beneficial tweaks and practical examples
At the heart of it with os
& glob
If your .txt
files are chilling in a non-current directory, do the switcheroo with os.chdir
:
Now, glob
fetches files from the switched directory. If you are multitasking with directories, avoid this as your scripts may trip over!
Embrace pathlib
for an OO touch
If you like things classy, the object-oriented pathlib
should be your pick:
pathlib
turns plain paths into Path objects that you can chain like there's no tomorrow!
Be mindful of sizes!
Working with tons of files? Your memory could be in peril! Try a generator expression:
Generators offer on-demand production. So bid adieu to memory overloads!
Dodge those common blunders!
File detection and error handling
While working with files, never underestimate the power of exceptions:
Good error handling can save the day when permissions issues or race conditions come knocking!
Being buddy-buddy with all platforms
Windows, Linux, macOS - paths differ across them! Leverage os.path
or pathlib
for the quirks of different OS parties.
Taking the knowledge a notch higher
Clearing up and sorting out files
You can use these techniques to organize your .txt
files, moving them to a chosen directory or maybe cleaning up old ones. George Washington would be proud!
Building a file inventory
For audit purposes, store all files along with their metadata (like size or last modified) in a CSV file. The CSV maestro strikes again!
Pipeline automation
Incorporate these directory scanning techniques into automation scripts, such as scanning directories for .txt
files and performing batch operations.
Was this article helpful?