file.read Problem

Brian Patton patton at fred.physik.uni-dortmund.de
Tue Aug 28 17:35:45 CEST 2001


Hi!

I'm having a problem with file.read.

I'm writing a filter to get data from a binary formatted file so that I
can import it into other software. I'm running Python 2.1 with the win
extensions downloaded from ActiveState. Win98 system. Oh, and I've
opened the file "rb"! ;)

My problem is that when I try to read in a given number of bytes, the
length of the string returned is rarely the length that is expect.
Furthermore, if I try to get the data by repeatedly grabbing smaller
chunks then is seems to step almost randomly through the file.

Some code:
print "Size I want to read: "+ str(sizes)+" Curr File Position: "+
str(fFile.tell())
                fFile.seek(8000)
                while fFile.tell()<12000:
                    test =fFile.read(6)
#struct.unpack(types,fFile.read(sizes))
                    print "Length of string read in: " + str(len(test))
+ " Current File Pos:" + str(fFile.tell())

Returns sensible stuff for a while, then:
Length of string read in: 6 Current File Pos:8228
Length of string read in: 6 Current File Pos:8234
Length of string read in: 6 Current File Pos:12099

Repeating the above with but with fFile.seek(8234) (the last good value)
gives:
Length of string read in: 3 Current File Pos:8746
Length of string read in: 6 Current File Pos:12336

Similar strangeness occurs when I try to read over 617 bytes at a time
(yup, I've experimented on the last bit)

Any ideas? (And thanks in advance)

Brian






More information about the Python-list mailing list