file.read Problem

Brian Patton patton at fred.physik.uni-dortmund.de
Wed Aug 29 11:50:29 CEST 2001


Thanks for the help - it's all sorted now.

I got confused between my tests in the interactive window and the script
itself (which was missing the "rb". Feel a bit silly now!)

Thanks again,

Brian

Tim Peters wrote:
> 
> [Brian Patton]
> > 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.
> > ...
> 
> Since this is "an impossible problem", I doubt you're going to get anywhere
> until you post a complete, self-contained, executable code snippet.
> Whatever is going wrong is going wrong due to something you haven't shown
> us.
> 
> Here's my complete, self-contained, executable code snippet, in file
> seek.py:
> 
> fFile = open('python.exe', 'rb')
> 
> fFile.seek(8000)
> while fFile.tell() < 12000:
>     test = fFile.read(6)
>     print "Length of string read in:", len(test), \
>           "Current File Pos:", fFile.tell()
> 
> Here I'm running it:
> 
> C:\Code\python\PCbuild>python seek.py > seek.txt
> 
> Here's what's in seek.txt afterwards:
> 
> Length of string read in: 6 Current File Pos: 8006
> Length of string read in: 6 Current File Pos: 8012
> Length of string read in: 6 Current File Pos: 8018
> ... many equally boring lines skipped ...
> Length of string read in: 6 Current File Pos: 11990
> Length of string read in: 6 Current File Pos: 11996
> Length of string read in: 6 Current File Pos: 12002
> 
> Everything's fine.  Show us something that isn't, including the open.



More information about the Python-list mailing list