Skip to content

Instantly share code, notes, and snippets.

@lucrib
Created March 21, 2017 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucrib/65b79fc7f7256f2841932e541dfca91c to your computer and use it in GitHub Desktop.
Save lucrib/65b79fc7f7256f2841932e541dfca91c to your computer and use it in GitHub Desktop.
A small script that receives a folder path and a extension and list all files of that extension recursively starting in the folder informed
import os
import sys
def list_files_in(path, ext=None):
for root, subdirs, files in os.walk(path):
# print('\n\nroot = ' + root)
for subdir in subdirs:
list_files_in(subdir)
for filename in files:
if extension and filename.lower().endswith(ext.lower()):
file_path = os.path.join(root, filename)
print(file_path)
if __name__ == '__main__':
try:
starting_path = str(sys.argv[1])
extension = str(sys.argv[2])
except IndexError as ie:
print 'Usage:\n\t' + sys.argv[0] + ' starting_path filter_extension'
sys.exit(0)
print('walk_dir = ' + starting_path)
print('walk_dir (absolute) = ' + os.path.abspath(starting_path))
list_files_in(starting_path, extension)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment