Created
April 4, 2016 10:25
-
-
Save abhijitmamarde/07e09bc83db62df9fd2c037d7e03ba1c to your computer and use it in GitHub Desktop.
Search files matching with patterns from current directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import fnmatch | |
import os | |
import sys | |
def find_files(filepath, patterns): | |
matches = 0 | |
if os.path.exists(filepath): | |
for root, dirnames, filenames in os.walk(filepath): | |
for pattern in patterns: | |
for filename in fnmatch.filter(filenames, pattern): | |
matched_file = os.path.realpath( | |
os.path.join(root, filename)) | |
print matched_file | |
matches = matches + 1 | |
print("{} file(s) found.".format(matches)) | |
else: | |
print("Error: Invalid Path") | |
if __name__ == '__main__': | |
file_patterns = ["*"] | |
if len(sys.argv) > 1: | |
file_patterns = sys.argv[1:] | |
find_files(".", file_patterns) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment