Recursively search for files and directories in Python and output

When recursively creating a list of all files and directories in Python, it's easier to create a function that combines ʻos.walk () and a generator (yield`).

For example, this directory structure

$ tree /tmp/test
├── 1
│   ├── a
│   │   ├── A
│   │   ├── B
│   │   └── hoge
│   └── b
│       ├── A
│       ├── B
│       └── hoge
└── 2
    ├── bar
    ├── c
    │   ├── buzz
    │   └── fizz
    └── foo

9 directories, 6 files
import os

def find_all_files(directory):
    for root, dirs, files in os.walk(directory):
        yield root
        for file in files:
            yield os.path.join(root, file)

for file in find_all_files('/tmp/test'):
    print file



Related article

Recommended Posts

Recursively search for files and directories in Python and output
Export and output files in Python
Search for strings in Python
Search for strings in files
Manipulate files and folders in Python
Check and move directories in Python
Output tree structure of files in Python
Search and play YouTube videos in Python
Type annotations for Python2 in stub files!
Read and write JSON files in Python
Sample for handling eml files in Python
Causal reasoning and causal search with Python (for beginners)
Reading and writing CSV and JSON files in Python
PDF files and sites useful for learning Python 3
Binary search in Python
Linear search in Python
Japanese output in Python
Linux: files and directories
Binary search in Python (binary search)
[Python selenium] After scraping Google search results, output title and URL in csv
[Python] Output battles and combinations (nesting for statements and if statements)
Include and use external Kv files in Python Kivy
Problems and countermeasures for Otsu's binarization overflow in Python
[Introduction for beginners] Reading and writing Python CSV files
Implement Depth-First Search (DFS) and Breadth-First Search (BFS) in python
How to use the grep command to recursively search directories and files to a specified depth
Linux (about files and directories)
Techniques for sorting in Python
Binary search in Python / C ++
Algorithm in Python (binary search)
Stack and Queue in Python
Unittest and CI in Python
[Python] Depth-first search and breadth-first search
About "for _ in range ():" in python
Read Fortran output in python
List method argument information for classes and modules in Python
Application to display and search local memos (diary) in Python
Sort and output the elements in the list as elements and multiples in Python.
Tips for coding short and easy to read in Python
Useful tricks related to list and for statements in Python
Problems and solutions when asked for MySQL db in Python 3
Google search for the last line of the file in Python
Check what the character code is for all files under the directory that is Python and output
Check for memory leaks in Python
Write a binary search in Python
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Output 2017 Premium Friday list in Python
Check for external commands in python
View photos in Python and html
Recursively unzip zip files with python
Sorting algorithm and implementation in Python
About dtypes in Python and Cython
Read and use Python files from Python
uproot: Python / Numpy based library for reading and writing ROOT files
How to use functions in separate files Perl and Python versions
Handling of JSON files in Python
Download Google Drive files in Python
Assignments and changes in Python objects
Make standard output non-blocking in Python
Algorithm in Python (depth-first search, dfs)