python-scripts/scripts/10_find_files_recursively.py

32 lines
821 B
Python
Raw Normal View History

2014-05-18 19:05:31 +02:00
import fnmatch
import os
# constants
PATH = './'
2016-12-17 17:22:43 +01:00
PATTERN = '*.md'
2014-05-18 19:05:31 +02:00
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)))
2014-05-18 19:05:31 +02:00
output_files(matches)
else:
print("No files found.")
2014-05-18 19:05:31 +02:00
else:
print("Sorry that path does not exist. Try again.")
2014-05-18 19:05:31 +02:00
def output_files(list_of_files):
for filename in list_of_files:
print(filename)
2014-05-18 19:05:31 +02:00
if __name__ == '__main__':
2016-12-17 17:22:43 +01:00
get_file_names(PATH, PATTERN)