Hi All,
Good day to everyone!
I'm new to Python (and relatively new to scripting, at that), and really benefited from looking at examples on this site, you guys are great!
I'm trying to do the following, and need help please:
1. Read an input file, each line formatted as <file location>,<search term>
2. Loop through the input file.
a. For each <file location>,<search term>, split them out by the comma delimiter.
b. Read the file at the location and search through it on the search term.
c. If a match exists on a search term, print both that line and the next line over.
def filereader (filename):
file = open(filename ,'r')
text = file.readlines()
file.close()
return text
def filewriter (filename, text):
file = open(filename ,'w')
file.writelines(text)
file.close()
def fileappender (filename, text):
file = open(filename, 'a')
file.writelines(text)
file.close()
.....
resultsfile = C:\\resultsfile.txt
results = filewriter(resultsfile, "Results File \n")
notes = filereader('C:\\mainlist.txt')
for item in notes:
print "Line: " + item
file = item.split(",")[0]
print "File: " + file
term = item.split(",")[1]
print "Term: " + term
fileappender(resultsfile, "File : " + file + "\n")
fileappender(resultsfile, "Error: " + term + "\n")
readfile = filereader(file)
#print readfile
counter = 0
for subline in readfile:
print "subline:" + subline
if counter == 1:
fileappender(resultsfile, " " + subline + "\n")
print "c0sub: " + subline
counter = 0
if subline.find(term) != -1:
fileappender(resultsfile, "Instance: " + subline + "\n")
print "c1sub: " + subline
counter = 1
Here's my issue:
The print statements seem to show that in my loop I can find and read each file location (log file). However, each of the files I'm testing with should have a match with the search term, but I only see written results for the last file. Like if I read three files, I don't see search term hits on the first two files, but I do see hits on the last file and see them output properly.
Could someone please take a look and let me know needs to be fixed? I think there's something wrong with my filereading, but not sure where/what.
Thanks in advance!