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!

Recommended Answers

All 2 Replies

First, don't use "file" as a variable name as it is a reserved word. I would guess that the problem may be with counter equal or not equal to one, or not stripping spaces from the name or term. There were some punctuation errors in the code you posted as well. The following code, using test data, seemed to work for me with the above errors corrected. Finally, "fileappender" is unnecessary and slow in that you have to open and close the file each time. Just return the file pointer from the open() and use it to write.

def filereader (filename):
    """  commented -- using test data
    
    fp = open(filename ,'r')
    text = fp.readlines()
    fp.close()
    """

    text = ["fname1, term1", "fname2, term2", "fname3, term3" ]
    return text
 
def filewriter (filename, text):
    fp = open(filename ,'w')
    return fp
#    file.writelines(text)    
#    file.close()
 
 
resultsfile = 'C:\\resultsfile.txt'
fp_out = filewriter(resultsfile)
fp_out.write("Results File \n")

notes = filereader('C:\\mainlist.txt')

readfile_test = [ ["term1", "term4", "term1"],
                  ["term1", "term2", "term1"],
                  ["term1", "term4", "term3"] ]
ctr = 0
 
for item in notes:
    print "Line: " + item
    substrs = item.split(",")
    fname = substrs[0].strip()
    print "File: " + fname
    term = substrs[1].strip()
    print "Term: " + term
 
    fp_out.write("File : %s\n" % (fname))
    fp_out.write("Error: %s\n" % (term))
 
    ##--- use test data
    readfile = readfile_test[ctr]
    ctr += 1
#    readfile = filereader(fname)
    #print readfile
 
    counter = 0
 
    for subline in readfile:
        print "subline:" + subline
        if counter == 1:
            fp_out.write( "          %s\n" % (subline))
            print "     c0sub: " + subline
            counter = 0
        if term in subline:
            fp_out.write("Instance: %s\n" % (subline))
            print "     c1sub: " + subline
            counter = 1

fp_out.close()

Hi woooee,
Thanks for the guidance and the adjusted code, I'm off to give it a shot!
Have a great day!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.