I always forget it, so make a note.
myfind.py
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), args.name) ]
files.extend(new_files)
# print
for f in files:
print f
# ls -F .
hoge/ uga/ ponyo.txt
# ls -F hoge
hogera.py honyara.sh
# ls -F uga
uga.sh ugera.py
# python myfind.py . --name "*.py"
./hoge/hogera.py
./uga/ugera.py
Recommended Posts