Recursively search the specified directory to see the file

Implemented using ʻos.walk () `

import argparse
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)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "directory", type=str, help="directory that you hope to look into."
    )
    parser.add_argument(
        "-e", "--extension", type=str, help="specify files with extension"
    )
    parser.add_argument("-c", "--cat", help="cat the file", action="store_true")
    parser.add_argument("-w", "--wc", help="wc the file", action="store_true")
    args = parser.parse_args()

    for file in find_all_files(args.directory):
        if args.extension:
            extension = file.split(".")[-1]
            if extension != args.extension:
                continue

        if args.wc:
            os.system("wc " + file)
        else:
            print(file)

        if args.cat:
            print("##### HEAD OF THE FILE #####\n\n")
            for line in open(file):
                print(line, end="")
            print("##### END OF THE FILE #####\n\n")


if __name__ == "__main__":
    main()

How to use

$ python find_all_files.py --help
usage: find_all_files.py [-h] [-e EXTENSION] [-c] [-w] directory

positional arguments:
  directory             directory that you hope to look into.

optional arguments:
  -h, --help            show this help message and exit
  -e EXTENSION, --extension EXTENSION
                        specify files with extension
  -c, --cat             cat the file
  -w, --wc              wc the file

You can recursively search for .py files in the specified directory to see their entire contents:

python find_all_files.py directory -e py -c

Referenced site

https://qiita.com/suin/items/cdef17e447ceeff6e79d

Recommended Posts

Recursively search the specified directory to see the file
Search the file name including the specified word and extension in the directory
I want to see the file name from DataLoader
File access under the directory
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 see the contents of the Jupyter notebook ipynb file
Python> __init__.py> Required to handle the specified directory as a package (empty file is acceptable)
Set the last modified date of the child file to the modified date of the parent directory
Script to generate directory from json file
Search for files with the specified extension
[linux] Split files to the specified size
Get the path to the systemd unit file
[Python] Read the specified line in the file
I want to get the path of the directory where the running file is stored.
How to read all the classes contained in * .py in the directory specified by Python
Extract only the file name excluding the directory in the directory
Delete all pyc files under the specified directory
Recursively search all files with multiple specified extensions
Save the object to a file with pickle
How to read a file in a different directory
Save the search results on Twitter to CSV.
Use Pandas to write only the specified lines of the data frame to an excel file
[Linux] File search
Output the specified table of Oracle database in Python to Excel for each file
Template of python script to read the contents of the file
How to delete the specified string with the sed command! !! !!
How to know the current directory in Python in Blender
I tried to touch the CSV file with Python
Read the xml file by referring to the Python tutorial
Set the specified column of QTableWidget to ReadOnly StyledItemDelegate
Resize the image to the specified size and blacken the margins
How to create a new file when the specified file does not exist — write if the file exists