Search code examples
pythonfilepathglobos.walkdirectory-tree

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.


Solution

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