file.write[lines]() weirdness

A. Jones netzapper at magicstar.net
Tue Apr 2 03:50:56 EST 2002


I'm sure I'm a fool... but, I've spent about three hours now playing
with this code, trying to get it all worked out.

The code will execute correctly the first time it's run (with the
program creating all the files fresh).  However, each time after that
(whether in the same session or if I close the module and reload it),
it garbles what it's written before, and only the newly created
entries into qodDB are readable.  

It all works excellently, except for this one little problem (it's
even fast enough for me).  The file lengths come up with "correct"
numbers, so it's seeking to (what I think is) the correct place before
it starts writing.

Any insight would be much appreciated.

Aubrey Jones

(code below:
default values for global variables:
qodIndex : {}
localCount : 0)

def chunkSynch(start, stop):
    global qodIndex
    global localCount
    global qodDB
    del qodDB #closes local database from __main__ call open(,'r')
    bmeCount =
int(urllib.urlopen("http://cgi.bmezine.com/qod3.exe?search=/totalqa").readline()[1:-3])
#gets QA count
    if bmeCount > 0: #up to date?
            try: #retrieves QODv3.0 online database to filelike object
                qodNT =
urllib.urlopen("http://cgi.bmezine.com/qod3.exe?search=/retrieveqa&start="
+ `start` + "&finish=" + `stop`)
            except IOError: #error handler for connection problems
                print "Problem connecting to database."
                return None
    else: #already up to date.
        print "Local database up to date."
        return None  
###TROUBLE STARTS HERE
    curLength = os.stat("QOD.qdb")[6]
    print curLength
    cumLength = 0
    qodDB = file("QOD.qdb", 'w') #opens QOD local database file in
write mode
    qodDB.seek(curLength)
    while 1: #writes local entries
        temp = qodNT.readline() #holds the readline value temporarily
        if temp == "": #checks for end of file
            break
        qodDB.writelines([temp])
        temp1 = eval("(" + temp[0:-2] + ")") #I'm proud of this
workaround
        qodIndex[int(temp1[0])] = curLength + cumLength
        cumLength = cumLength + len(temp) + 1
        print cumLength
### I THINK THE TROUBLE ENDS HERE
    localCount = stop
    del qodDB
    del qodNT
    qodDB = file("QOD.qdb", 'r')


-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 100,000 Newsgroups - Ulimited downloads - 19 servers ==-----



More information about the Python-list mailing list