-
Notifications
You must be signed in to change notification settings - Fork 593
/
Copy pathfilesearch.py
145 lines (121 loc) · 3.79 KB
/
filesearch.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import datetime
import argparse
from progress.bar import Bar
class FileSearch:
def __init__(self):
self.file_count = 0
self.progress_bar = Bar('Searching', suffix='%(eta)ds')
def get_file_count(self, path, is_folder):
if not is_folder:
print("[INFO] Reading files ...")
else:
print("[INFO] Reading folders ...")
self.file_count = 0
for root, dirs, files in os.walk(path):
if is_folder:
self.file_count += len(dirs)
else:
self.file_count += len(files)
def __perform_search(
self,
search_term,
start_dir,
is_folder,
disable_case,
show_bar
):
results = []
for root, dirs, files in os.walk(start_dir):
if is_folder:
for dir in dirs:
if show_bar:
self.progress_bar.next()
if disable_case and search_term in dir:
results.append(os.path.join(root, dir))
elif not disable_case and search_term.lower() in dir.lower():
results.append(os.path.join(root, dir))
else:
for file in files:
if show_bar:
self.progress_bar.next()
if disable_case and search_term in file:
results.append(os.path.join(root, file))
elif not disable_case and search_term.lower() in file.lower():
results.append(os.path.join(root, file))
return results
def search(
self,
search_term,
start_dir,
is_folder=False,
disable_case=False,
show_bar=True
):
self.file_count = 0
self.get_file_count(start_dir, is_folder)
self.progress_bar.max = self.file_count
if not os.path.exists(start_dir):
raise Exception(f"Path {start_dir} does not exist.")
start_time = datetime.datetime.now()
result = self.__perform_search(
search_term,
start_dir,
is_folder,
disable_case,
show_bar
)
end_time = datetime.datetime.now()
if not is_folder:
print(
f"\n\n[INFO] Searched {self.file_count:,} files in {(end_time - start_time).total_seconds()} sec")
else:
print(
f"\n\n[INFO] Searched {self.file_count:,} folders in {(end_time - start_time).total_seconds()} sec")
return result
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Search files within your computer")
parser.add_argument(
"search-term",
metavar="term",
type=str,
help="Filename/Word to search for"
)
parser.add_argument(
"start-dir",
metavar="dir",
type=str,
help="Directory where you want to search"
)
parser.add_argument(
"-dc",
"--disable-case",
action="store_true",
help="Perform case insensitive search"
)
parser.add_argument(
"-f",
"--folder",
action="store_true",
help="Search only folders (default: searches files)"
)
parser.add_argument(
"-nb",
"--no-bar",
action="store_false",
help="Not show progress bar"
)
args = parser.parse_args().__dict__
searcher = FileSearch()
results = searcher.search(
args['search-term'],
args['start-dir'],
args['folder'],
args['disable_case'],
args['no_bar']
)
print("[INFO] Results: \n")
for path in results:
print("Path:", path)
print("")