-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_word.py
executable file
·50 lines (38 loc) · 1.08 KB
/
find_word.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
#!/usr/bin/python3
import sys
def main():
try:
options = ['-i', '--ignore-case']
fhand = open(sys.argv[-1])
word = sys.argv[(-1) - 1]
if word.startswith('-'):
raise Exception(getUsage())
elif sys.argv[1] not in options and len(sys.argv) == 4:
print(sys.argv[1])
raise Exception(getUsage())
except:
raise Exception(getUsage())
x = findWord(word, fhand)
for line in x:
print(line)
def findWord(word, file):
arguments = sys.argv
lineList = []
if arguments[1] == '-i' or arguments[1] == '--ignore-case':
ignoreCase = True
else:
ignoreCase = False
if ignoreCase:
word = word.lower()
for line1 in file:
if word in line1.lower():
lineList.append(line1)
else:
for line1 in file:
if word in line1:
lineList.append(line1)
return lineList
def getUsage():
return f'usage: {sys.argv[0]} [-i | --ignore-case ignore case] <word> <file>'
if __name__ == "__main__":
main()