From 239a0ff7fff87cc9d55a1299589efdd7ce798c7b Mon Sep 17 00:00:00 2001 From: Michael Herman Date: Sun, 18 May 2014 11:05:31 -0600 Subject: [PATCH] file 10 --- 10_find_files_recursively.py | 31 +++++++++++++++++++++++++++++++ readme.md | 3 ++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 10_find_files_recursively.py diff --git a/10_find_files_recursively.py b/10_find_files_recursively.py new file mode 100644 index 0000000..7251b10 --- /dev/null +++ b/10_find_files_recursively.py @@ -0,0 +1,31 @@ +import fnmatch +import os + +# constants +PATH = '/../../../..' +PATTERN = '*.py' + + +def get_file_names(filepath, pattern): + matches = [] + if os.path.exists(filepath): + for root, dirnames, filenames in os.walk(filepath): + for filename in fnmatch.filter(filenames, pattern): + # matches.append(os.path.join(root, filename)) # full path + matches.append(os.path.join(filename)) # just file name + if matches: + print "Found {} files:".format(len(matches)) + output_files(matches) + else: + print "No files found." + else: + print "Sorry that path does not exist. Try again." + + +def output_files(list_of_files): + for filename in list_of_files: + print filename + + +if __name__ == '__main__': + all_files = get_file_names(PATH, PATTERN) \ No newline at end of file diff --git a/readme.md b/readme.md index 07c1759..217a3f2 100644 --- a/readme.md +++ b/readme.md @@ -8,4 +8,5 @@ 1. **06_execution_time.py**: class used for timing execution of code 1. **07_benchmark_permissions_loading_django.py**: benchmark loading of permissions in Django 1. **08_basic_email_web_crawler.py**: web crawler for grabbing emails from a website recursively -1. **08_basic_link_web_crawler.py**: web crawler for grabbing links from a website recursively +1. **09_basic_link_web_crawler.py**: web crawler for grabbing links from a website recursively +1. **10_find_files_recursively.py**: recursively grab files from a directory