-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind.py
More file actions
executable file
·59 lines (50 loc) · 1.71 KB
/
find.py
File metadata and controls
executable file
·59 lines (50 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import fnmatch
import os
import re
import sys
def main():
config = parse_args()
pattern = config.pattern
pattern_normcase = os.path.normcase(pattern)
pattern_regex = fnmatch.translate(pattern_normcase)
match_expr = re.compile(pattern_regex)
dir_paths = config.dir_path
if not dir_paths:
dir_paths = [os.getcwd()]
run(match_expr, dir_paths)
def run(match_expr, dir_paths):
for dir_path in dir_paths:
for (cur_dir_path, dirnames, filenames) in os.walk(dir_path):
for filename in filenames:
filename_normcase = os.path.normcase(filename)
match = match_expr.match(filename_normcase)
if match is not None:
cur_path = os.path.join(cur_dir_path, filename)
print(cur_path)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("pattern",
help="""The pattern of the filename to search for;
may contain wildcard characters * and ?"""
)
parser.add_argument("dir_path",
nargs="*",
help="""The paths of the directories to search for the given path;
may be specified more than once, in which case each directory will
be searched in turn; if not specified, the current directory will
be searched"""
)
parsed_args = parser.parse_args()
return parsed_args
if __name__ == "__main__":
try:
exit_code = main()
except KeyboardInterrupt:
print("ERROR: application terminated by keyboard interrupt",
file=sys.stderr)
exit_code = 1
sys.exit(exit_code)