Find files like find on linux in Python

I always forget it, so make a note.

import os
import fnmatch
import argparse

# parsing arguments
parser = argparse.ArgumentParser()
parser.add_argument('path', type=str, default=None,
    help="Directory for searching files.")
parser.add_argument('-n', '--name', type=str, default='*',
    help="Pattern for searching files.")
args = parser.parse_args()

# search
files = list()
for root, dirs, names in os.walk(args.path):
    new_files = [ os.path.join(root, f) for f in names
        if fnmatch.fnmatch(os.path.join(root, f), ]

# print
for f in files:
    print f
# ls -F .
hoge/	uga/	ponyo.txt
# ls -F hoge
# ls -F uga
# python . --name "*.py"

Recommended Posts

Find files like find on linux in Python
Find large files / directories on Linux
How to find large files on Linux
List find in Python
Find the difference in Python
Find permutations / combinations in Python
Let's find pi in Python
Transpose CSV files in Python Part 1
Install Python Pillow on Amazon Linux
Something like JS setTimeout in python
Find memory-consuming lists / arrays on Python
Manipulate files and folders in Python
Handling of JSON files in Python
Download Google Drive files in Python
Display characters like AA in python
Sort large text files in Python
Read files in parallel with Python
Export and output files in Python
Introduce Python 3.5.2 environment on Amazon Linux
python> os.path.join ('data','checkpoint')>'data / checkpoint' on linux
Something like tail -f in Python
Extract strings from files in Python
Create your own Linux commands in Python
Output tree structure of files in Python
Notes on nfc.ContactlessFrontend () for nfcpy in python
Notes on creating static files in Django
Type annotations for Python2 in stub files!
[C] [python] Read with AquesTalk on Linux
Run AzureKinect in Python on Christmas Eve.
Referencing INI files in Python or Ruby
Automate jobs by manipulating files in Python
Download files on the web with Python
Read and write JSON files in Python
Find and check inverse matrix in Python
Sample for handling eml files in Python
Find (deterministic finite) Cartesian automata in Python
Do something like Redis transactions in Python
Install rJava on Linux in R3.6 environment.
Download files in any format using Python
Try something like Python for-else in Ruby
Install Python 3.8, Pip 3.8 on EC2 (Amazon Linux 2)
Notes on using code formatter in Python
Run Python in C ++ on Visual Studio 2017
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Convert Unicode escape sequences like \ u in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
[Python] Get the files in a folder with Python
Convert FBX files to ASCII <-> BINARY in Python
Meta-analysis in Python
Python on Windows
How to find out what kind of files are stored in S3 in Python
Unittest in python
twitter on python3
Summary of how to import files in Python 3
Install python package in personal environment on Ubuntu
Run Python YOLOv3 in C ++ on Visual Studio 2017
Resolve Japanese write error UnicodeEncodeError in Python files