-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram.py
More file actions
108 lines (88 loc) · 3.24 KB
/
Copy pathprogram.py
File metadata and controls
108 lines (88 loc) · 3.24 KB
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
""" before you start your app
1- create a directory to host the app
2- Initiliaze a git repo
3- create a readme file explaining what the app is about.
4- create a file called program.py which has the code below
5- add and commit to git
6- start implementing the functions after each function is completed,
commit your changes
7- have for each stage a different version
"""
# Stage 1
import os
import sys
import re
from collections import namedtuple
try:
input = raw_input
except NameError:
pass
myResults = namedtuple("myResults", 'file line')
def main():
print_header()
path = get_folder_user()
key = get_keyword_user()
rL, wC = return_results(path, key)
writeResults(rL, wC, key)
def print_header():
print(" ===================================== ")
print(" | WORD FINDER | ")
print(" ===================================== ")
def get_folder_user():
path = input("Please enter the path to your folder: ").rstrip()
if not os.path.isdir(path):
print("Please enter a valid path!")
sys.exit()
else:
return path
def get_keyword_user():
keyword = input("Please enter the word you want to find: ").rstrip()
if not keyword.strip():
print("Please enter a word to search!")
sys.exit()
else:
return keyword
# let's do this in two steps
# first if path is file then search
# if it is a directory just continue "ignore it"
# once we get it right then we can think about
# if it is a directory
# and since we are returning three item (file, line_no, text)
# let's create a data structure so we can manipulate them
# I will create a class or namedtuple to use
def return_results(path, keyword):
resultList = []
os.chdir(path)
word_count = 0
for file in os.listdir(path):
# print(">>>>> before filtering >>>>> {}".format(file))
if not os.path.isdir("{}/{}".format(path, file)):
if file != "Search_result.txt":
# print(">>>>>> after filtering >>>>>>> {} ".format(file))
with open(file, "r") as openfile:
for num, line in enumerate(openfile):
if re.search(r'\b({})\b'
.format(keyword.lower()), line.lower()):
word_count += 1
resultList.append(myResults(file, num+1))
print("File Name: {}, Line No: {}"
.format(file, num+1))
return resultList, word_count
def writeResults(resultList, word_count, keyword):
with open("Search_result.txt", "a") as results_file:
results_file.write("Word: {}, Word Count: {}\n".format(
keyword, str(word_count)))
results_file.write("-------------------------\n")
for item in resultList:
results_file.writelines("File: {}, Line: {}\n"
.format(item.file, item.line))
results_file.write("=========================\n")
if __name__ == '__main__':
main()
# Stage 2
# once you finish the first stage, then we can move to add argparse
# so it can run as one command line
# Stage 3
# add logging capability to the app
# Stage 4
# add unit testing