-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
61 lines (53 loc) · 1.41 KB
/
search.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
import sys
import os
from functions.functions import *
def main():
words = []
option = None
file_name = None
dir_name = None
"""accepting arguments from cmd"""
try:
if sys.argv[1] == "-f" or sys.argv[1] == "-d":
option = sys.argv[1]
else:
print "Please select \"-f\" for single text file or \"-d\" for Full directory search"
except:
print "Please select \"-f\" for single text file or \"-d\" for Full directory search"
sys.exit()
try:
if sys.argv[2].endswith(".txt") and option == "-f":
file_name = sys.argv[2]
elif option =="-d":
dir_name = sys.argv[2]
else:
print "please enter text file name or directory name with proper option"
except:
print "Please enter .txt file name"
sys.exit()
try:
if len(sys.argv) > 3:
for args in sys.argv[3:]:
if args not in words:
words.append(args)
else:
print "Please enter one or more words to search"
except:
print "Please enter one or more words to search"
sys.exit()
if option == "-f":
if os.path.isfile(file_name):
wordwithsynonyms(file_name, words)
else:
print "File " + file_name + " Not found."
elif option == "-d":
if os.path.isdir(dir_name):
files_in_dir = []
files_in_dir = all_files_in_dir(dir_name)
for files in files_in_dir:
print "Searching in file " + files + "\n"
wordwithsynonyms(files, words)
else:
print "directory " + dir_name + " not Found."
if __name__ == "__main__":
main()