loop does not count...

Jonathan Driller jdriller at orchid.org
Thu Nov 13 00:13:41 CET 2003

I am very new to this and would greatly appreciate some insight.
I am trying to learn Python by doing something useful; write a script
that will count and output my aggregated visits to my website. I have
a separate text file that holds the list of uri strings that I want to
count and then this code. The log is sampleLog.txt.

The problem is that it says all the preceding uris are 0 (they are
not) and only the last string actually is counted....why is that?

def stats():
    import sys
    import string
    #read the file of current urls - presumes it exists already
    x = open('urlList.txt')
    # note this reads as a file, not a list
    urlFile = x.read()
    # don't need to close but should
    #list what is in text file of urls
    print "Here is what we check now:\n", urlFile
    print "\n"
    # len(listName) gives # of list elements

    #turn url listings into list
    z = open('urlList.txt')
    urlList = z.readlines()
    #open log file
    log = open('sampleLog.txt')
    logFile = log.read()
    #initialize counter at 0
    i = 0
    # loop through to search for urls
    while i < len(urlList):
        # put element into var
        check = urlList[i]
        #print out # found and what it was
        print check, " found" , string.count(logFile, check) ,"times
        # increment for next item - can't do i ++
        i = i + 1

More information about the Python-list mailing list