简体   繁体   中英

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

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.

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()
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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM