Use os.walk to find a folder with specific file extensions under directory tree

819 views Asked by At

I need to traverse all my folders to find the file path of a folder with files with a specific extension (for sake of example, we will say .txt). I do not know if the folder will be at the top of the tree or at the bottom.

For example, we are starting in:

OneDrive/Documents/project/SourceCode

The folder containing all the .txt files could be in OneDrive/Documents/project/SourceCode/TxtFiles or it could be in OneDrive/Documents/project/TxtFiles or it could also be up more past the project file.

How would I find the filepath? I tried using os.walk, but I do not have a strong enough understanding of how it works. In the end, I am globbing all of the .txt files into a giant list.

2

There are 2 answers

0
PMende On BEST ANSWER

I would recommend using pathlib:

from pathlib import Path

base_path = Path('base/path/to/search/from')
text_file = next(base_path.glob('**/*.txt'))
parent_dir = text_file.parent.resolve()
0
kchawla-pi On
from pathlib import Path
from pprint import pprint
import os

def find_files_of_ext(root, ext):
    return [str(Path(dir, file_)) for dir, subdir, files in os.walk(root) for file_ in files if Path(file_).suffix == ext]


filepaths = find_files_of_ext('C:/Users/username/OneDrive', '.jpeg' )
pprint(filepaths)