-
Notifications
You must be signed in to change notification settings - Fork 0
/
File_Finder.py
192 lines (143 loc) · 5.77 KB
/
File_Finder.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import os
import concurrent.futures
def process(paths, keywords, exts, ignored_exts):
with concurrent.futures.ProcessPoolExecutor() as executor:
results = [executor.submit(
read_path, i + 1, paths[i], keywords, exts, ignored_exts) for i in range(len(paths))]
for f in concurrent.futures.as_completed(results):
print(f.result())
def thread(folders, i):
with concurrent.futures.ThreadPoolExecutor() as executor:
wf = open(f"Path {i} Results.txt", "a")
results = []
for folder_ in folders:
for file_ in folders[folder_]:
file_path = f"{folder_}\\{file_}"
try:
if not len(exts):
results.append(executor.submit(
read_file, file_path, keywords))
else:
file_ext = file_.split(".")[1].lower()
for ext in exts:
if not ignored_exts:
if ext.lower() == file_ext:
results.append(executor.submit(
read_file, file_path, keywords))
else:
if ext.lower() != file_ext:
results.append(executor.submit(
read_file, file_path, keywords))
except:
Exception()
for f in concurrent.futures.as_completed(results):
wf.write(f.result)
def get_paths_from_user_input():
example = r"C:\Program Files (x86)"
paths = []
path = input(
f"What is the top level directory you want to search from? \n- Tip: Type \"Done\" once you add the directories you want \n- Tip: Use the full path (e.g. {example})\n\n* ")
print()
if "Done" != path:
paths.append(path)
while "Done" != path:
path = input(
f"What is the top level directory you want to search from? \n- Tip: Type \"Done\" once you add the directories you want \n- Tip: Use the full path (e.g. {example})\n\n* ")
print()
if "Done" != path:
paths.append(path)
return paths
def get_paths_from_file():
paths = []
with open("Directories.txt") as rf:
for line in rf:
if "\n" in line:
line = line[:-1]
paths.append(line)
return paths
def get_keywords():
keywords = []
keyword = input(
"What is a keyword you want to search for? \n- Tip: Type \"Done\" once you add the keywords you want\n\n* ")
print()
if "Done" != keyword:
keywords.append(keyword)
while "Done" != keyword:
keyword = input(
"What is a keyword you want to search for? \n- Tip: Type \"Done\" once you add the keywords you want\n\n* ")
print()
if "Done" != keyword:
keywords.append(keyword)
return keywords
def get_wanted_exts():
exts = []
ext = input(
"(OPTIONAL) What is an entension type you want to search for? \n- Tip: Type \"Done\" once you add the extensions you want \n- Tip: Type the extension like \"txt\"\n\n* ")
print()
if "Done" != ext:
exts.append(ext)
while "Done" != ext:
ext = input(
"(OPTIONAL) What is an entension type you want to search for? \n- Tip: Type \"Done\" once you add the extensions you want \n- Tip: Type the extension like \"txt\"\n\n* ")
print()
if "Done" != ext:
exts.append(ext)
return exts
def get_ignored_exts():
exts = []
ext = input(
"(OPTIONAL) What is an entension type you want to ignore? \n- Tip: Type \"Done\" once you ignore the extensions you want \n- Tip: Type the extension like \"txt\"\n\n* ")
print()
if "Done" != ext:
exts.append(ext)
while "Done" != ext:
ext = input(
"(OPTIONAL) What is an entension type you want to ignore? \n- Tip: Type \"Done\" once you ignore the extensions you want \n- Tip: Type the extension like \"txt\"\n\n* ")
print()
if "Done" != ext:
exts.append(ext)
return exts
def read_file(path, keywords):
with open(path) as rf:
i = 0
data = ""
for line in rf:
i += 1
for word in keywords:
if word in line:
data += f"Path: {path} \nLine: {i} - {line}\n\n"
return data
def read_path(i, path, keywords, exts, ignored_exts):
print(f"Searching through {path}...")
folders = {}
main_folder = ""
for subdir, dirs, files in os.walk(path):
dir_name = str(subdir).split("/")
dir_name = dir_name[len(dir_name)-1]
if len(main_folder) < 1:
main_folder = dir_name
folders_and_files = []
for file_ in files:
folders_and_files.append(file_)
folders[dir_name] = folders_and_files
thread(folders, i)
return f"\n{path} complete!"
if __name__ == '__main__':
paths = get_paths_from_file()
keywords = get_keywords()
exts = []
ignored_exts = False
user_input = input(
"Yes or No. Do you want to search for a file type?\n\n* ")
print()
if user_input == "Yes":
exts = get_wanted_exts()
else:
user_input = input(
"Yes or No. Do you want to ignore a file type?\n\n* ")
print()
if user_input == "Yes":
ignored_exts = True
exts = get_ignored_exts()
process(paths, keywords, exts, ignored_exts)
input("\nPress Enter to close...")