[PYTHON] Search for files with the specified extension

Current directory only

import os
import glob


def find_all_files_with_ext(ext):
    pattarn = '*' + os.extsep + ext
    return glob.glob(pattarn)

Including subdirectories

import os


def _find_all_files_with_ext(dir, ext):
    suffix = os.extsep + ext.lower()
    for root, dirs, files in os.walk(dir):
        for file in files:
            if file.lower().endswith(suffix):
                yield os.path.join(root, file)


def find_all_files_with_ext(dir, ext):
    return list(_find_all_files_with_ext(dir, ext))

Reference material

Recommended Posts

Search for files with the specified extension
Recursively search all files with multiple specified extensions
[Boto3] Search for Cognito users with the List Users API
Search for strings in files
Search for files with line feed code CR + LF under the current directory
Search for large files on Linux from the command line
Install the data files with setup.py
Search for homeomorphic idioms with opencv
Delete tweets for the specified period
Search the file name including the specified word and extension in the directory
For the time being, I want to convert files with ffmpeg !!
Generate all files with a specific extension
Ask for Pi with the bc command
Search the maze with the python A * algorithm
[linux] Split files to the specified size
Heat Map for Grid Search with Matplotlib
Download files on the web with Python
The third night of the loop with for
The second night of the loop with for
[Reinforcement learning] Search for the best route
Search for Twitter keywords with tweepy and write the results to Excel
[Python] Get the files in a folder with Python
Causal reasoning and causal search with Python (for beginners)
Exposing the DCGAN model for Cifar 10 with keras
Delete all pyc files under the specified directory
Use logger with Python for the time being
I played with Floydhub for the time being
Recursively search the specified directory to see the file
I tried searching for files under the folder with Python by file name
How to use the grep command to recursively search directories and files to a specified depth
How to list files under the specified directory in a list (multiple conditions / subdirectory search)
How to set the development environment for each project with VSCode + Python extension + Miniconda
Extract the table of image files with OneDrive & Python
Add the attribute of the object of the class with the for statement
How to delete the specified string with the sed command! !! !!
Stop EC2 for specified time + start with Lambda (python)
Display the integrated temperature for each field with Z-GIS
Information for controlling the motor with Python on RaspberryPi
Extract files from EC2 storage with the scp command
Recursively search for files and directories in Python and output
[For beginners] Quantify the similarity of sentences with TF-IDF
Upload files with Django
Sequential search with Python
Binary search with python
Binary search with Python3
Database search with db.py
I wrote the code for Japanese sentence generation with DeZero
Try a similar search for Image Search using the Python SDK [Search]
[NetworkX] I want to search for nodes with specific attributes
Search for variables in pandas.DataFrame and get the corresponding row.
Process the files in the folder in order with a shell script
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Google search for the last line of the file in Python
Let's reduce the man-hours required for server setup with Ansible