-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_for_ext.py
49 lines (36 loc) · 1.46 KB
/
search_for_ext.py
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
#! /usr/bin/env python
'''A recursive, removal utility that focuses on files with a target extension.'''
import re, os, sys
def main():
print("Search by Extension Tool")
print("-------------------------------")
print("Code: Dorian 'deepwave' Pula")
'Check if there are enough arguments supplied.'
if check_args():
'Grab the arguments.'
trg_ext = sys.argv[1]
'Keep the current directory.'
curr_dir = os.getcwd()
'Set up the regex matching.'
matcher = re.compile("^.+\." + trg_ext + "$")
'Walk the directories, find matches and rename them'
for root, dirs, files in os.walk(curr_dir):
print("Searching in: " + root)
for name in files:
if matcher.match(name):
path = os.path.join(root, name)
print(path + " found.")
def check_args():
'''Checks if there are enough arguments to go on.'''
arg_num = len(sys.argv)
if arg_num < 2:
'Print out a pretty usage statement.'
print()
print("Usage:")
print("python search_by_ext.py <extension>")
print(" <extension>: Find files with this extension.")
print("NOTE: DO NOT INCLUDE THE . IN THE EXTENSIONS!")
return False
else:
return True
if __name__ == "__main__": main()