loop does not count...

Jonathan Driller jdriller at orchid.org
Tue Nov 18 18:48:15 CET 2003


That worked beautifully - except that I kept getting errors if I moved
the import statements out of the def... "invalid syntax"

So, now it is (with the changed comments per Terry's suggestion to be

def stats():
    import sys
    import string
    #read the file of current urls that are to be checked - 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"
    #prompt to add additional urls - use raw so quotes not nec
    addItem = raw_input('Enter an additional URL in quotes:' )
	# can't append() to a file, must list.append() or do this way
    urlList = urlFile + "\n" + addItem
    #write additional urls to list
    y = open('urlList.txt', 'w')
    # 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].strip()
        #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