Explain Codes LogoExplain Codes Logo

Find all files in a directory with extension .txt in Python

python
file-handling
directory-scan
glob
Anton ShumikhinbyAnton Shumikhin·Sep 11, 2024
TLDR

Engage glob for a quick win:

import glob print(glob.glob("**/*.txt", recursive=True)) # Catch 'em all!

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:

txt_files = glob.glob('*.txt') # Glob, tell me more about .txt files!

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:

import os # Traverse directory for root, dirs, files in os.walk("/yourdir"): # Walk the walk... for file in files: # Prize out .txt files if file.endswith(".txt"): # txt, the tale of lost files print(os.path.join(root, file)) # Once upon a root...

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:

txt_files = [os.path.join("/directory", f) for f in os.listdir("/directory") if f.endswith('.txt')] # Building paths, no bricks needed!

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:

os.chdir("/mydir") txt_files = glob.glob("*.txt") # Glob, have you found my .txt files? 🧐

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:

from pathlib import Path txt_files = Path("/yourdir").rglob('*.txt') # Directory, tell me .txt stories!

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:

txt_files_gen = (f for f in os.listdir("/yourdir") if f.endswith('.txt')) # Genie, get me .txt files!

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:

try: txt_files = glob.glob('*.txt') except Exception as e: print(f"Well, this happened: {e}") # Oops, did 'try' hit a 'wall'?

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.