Thursday, January 30, 2025
HomeProgrammingHow Can I Recursively Find All Files in Current and Subfolders

How Can I Recursively Find All Files in Current and Subfolders

To recursively find all files in the current directory and its subdirectories in Python, you can use the os module or pathlib. Using os.walk() is a common approach, which traverses through all directories and subdirectories. Here’s an example:

python
import os

for root, dirs, files in os.walk('.'):
for file in files:
print(os.path.join(root, file))

Alternatively, using pathlib.Path.rglob() simplifies the process:

python
from pathlib import Path

for file in Path('.').rglob('*'):
if file.is_file():
print(file)

Both methods will return the complete file paths recursively.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x