file.read() doesn't read the whole file

R. David Murray rdmurray at bitdance.com
Tue Mar 24 01:37:14 CET 2009


Steve Holden <steve at holdenweb.com> wrote:
> Sreejith K wrote:
> >> Try and write an example that shows the problem in fifteen lines or
> >> less. Much easier for us to focus on the issue that way.
> > 
> > import os
> > def read(length, offset):
> > 	os.chdir('/mnt/gfs_local/')
> > 	snap = open('mango.txt_snaps/snap1/0','r')
> > 	snap.seek(offset)
> > 	data = snap.read(length)
> > 	print data
> > 
> > read(4096,0)
> > 
> > This code shows what actually happens inside the code I've written.
> > This prints the 4096 bytes from the file '0' which is only 654 bytes.
> > When we run the code we get the whole file. That's right. I also get
> > it. But when this read() function becomes the file class read()
> > function in fuse, the data printed is not the whole but only a few
> > lines from the beginning.
> 
> This is confusing. I presume you to mean that when you make this
> function a method of some class it stops operating correctly?
> 
> But I am not sure.
> 
> I am still struggling to understand your problem. Sorry,it's just a
> language thing. If we take our time we will understand each other in the
> end.

You may be asking this question for pedagogical reasons, Steve, but
in case not...the OP is apparently doing a 'less xxxx' where xxxx is
the name of a file in a fuse filesystem (that is, a mounted filesystem
whose back end is some application code written by the OP).  So when
the OP runs less, several calls get made to fuse, which passes them to
fuse-python, which calls methods on the OP's python class.  He is looking
in particular at the 'read' call, which happens after 'less' has opened
the file and wants to read a block (apparently either less or fuse is
asking for the first 4096 bytes of the file).  At that point his 'read'
method above is called.  But based on what he's told us it appears his
conclusion that the 'snap.read(length)' call is not returning the whole
file is based on the fact that less is only showing him part of the file.
There are several steps between that 'snap.read' and less displaying on
the terminal whatever bytes it got back from its read call in whatever
way it is less chooses to display them....

--
R. David Murray           http://www.bitdance.com




More information about the Python-list mailing list