[Tutor] Needing help with variables

Cain, Steven Steven.Cain at norfolk.gov
Wed Apr 23 20:55:20 CEST 2008


I have the code below and I am in need of a little help with variables.
What I am trying to do is substitute a variable where it says insert
variable. I actually need to place 3 variables. 1 for date, 1 for time
and 1 for a string of text.

Any help would be greatly appreciated.

Steve

def BackwardsReader(file, BLKSIZE = 4096):
    #"""Read a file line by line, backwards"""

    buf = ""
    file.seek(-1,2)
    lastchar = file.read(1)
    trailing_newline = (lastchar == "\n")
    
    while 1:
        newline_pos = buf.rfind("\n")
        pos = file.tell()
        if newline_pos != -1:
            # Found a newline
            line = buf[newline_pos+1:]
            buf = buf[:newline_pos]
            if pos or newline_pos or trailing_newline:
                line += "\n"
            yield line
        elif pos:
            # Need to fill buffer
            toread = min(BLKSIZE, pos)
            file.seek(-toread, 1)
            buf = file.read(toread) + buf
            file.seek(-toread, 1)
            if pos == toread:
                buf = "\n" + buf
        else:
            # Start-of-file
            return


for line in BackwardsReader(open('stopServer.log')):
    if line[0] == '[':
        print line[1:8],line[9:16],line[62:93] ( insert Variable )
        break



More information about the Tutor mailing list