Created
April 9, 2019 23:31
-
-
Save sremy/6a5772a9ce8ec496f3afa5ea70f5c46d to your computer and use it in GitHub Desktop.
Python script to check if each pattern matches at least one filename
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/python | |
| # Finds files based on patterns (supporting wildcards *?[-]) | |
| # Prints matching filenames or otherwise "NOT FOUND" | |
| # Exits with code 1 if at least one pattern doesn't match | |
| from __future__ import print_function | |
| import glob | |
| import sys | |
| import os | |
| pattern_list = ["LIP$DATE", | |
| "thefile.txt", | |
| "*$VAR1*", | |
| "*.py"] | |
| def replace_value(motif): | |
| motif = motif.replace("$DATE", "20190331") | |
| motif = motif.replace("$VAR1", "VALUE1") | |
| return motif | |
| def find_file(pattern): | |
| dirname = os.path.expanduser("~/data/read/") | |
| return glob.glob1(dirname, pattern) | |
| all_found = True | |
| for p in pattern_list: | |
| assigned_value = replace_value(p) | |
| matching_list = find_file(assigned_value) | |
| if len(matching_list) == 0: | |
| print("[%s]: NOT FOUND" % assigned_value, file=sys.stderr) | |
| all_found = False | |
| else: | |
| print("[%s]: %s" % (assigned_value, str(matching_list))) | |
| if all_found: | |
| sys.exit(0) | |
| else: | |
| sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment