Skip to content

Instantly share code, notes, and snippets.

@abhijitmamarde
Created April 4, 2016 10:25
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 abhijitmamarde/07e09bc83db62df9fd2c037d7e03ba1c to your computer and use it in GitHub Desktop.
Save abhijitmamarde/07e09bc83db62df9fd2c037d7e03ba1c to your computer and use it in GitHub Desktop.
Search files matching with patterns from current directory.
#!/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