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

Sreejith K sreejithemk at gmail.com
Fri Mar 20 15:03:35 CET 2009


On Mar 20, 4:43 pm, "R. David Murray" <rdmur... at bitdance.com> wrote:
> Sreejith K <sreejith... at gmail.com> wrote:
> > Hi,
>
> > >>> snapdir = './mango.txt_snaps'
> > >>> snap_cnt = 1
> > >>> block = 0
> > >>> import os
> > >>> os.chdir('/mnt/gfs_local')
> > >>> snap = open(snapdir + '/snap%s/%s' % (repr(snap_cnt), repr(block)),'r')
> > >>> snap.read()
> > 'dfdfdgagdfgdf\ngdgfadgagadg\nagafg\n\nfs\nf\nsadf\n\nsdfsdfsadf\n'
> > >>> snapdir + '/snap%s/%s' % (repr(snap_cnt), repr(block))
> > './mango.txt_snaps/snap1/0'
>
> > The above code works fine and it reads the whole file till EOF. But
> > when this method is used in a different scenario the file is not read
> > completely. I'll post the code that read only some part of the file...
>
> > self.snap = open(self.snapdir + '/snap%d/%d' % (self.snap_cnt,
> > block),'r') ## opens /mnt/gfs_local/mango.txt_snaps/snap1/0
> > self.snap.seek(off%4096) ## seeks to 0 in this case
> > bend = 4096-(off%4096) ## 4096 in this case
> > if length-bend <= 0:    ## true in this case as length is 4096
> >    tf.writelines("returned \n")
> >    data = self.snap.read(length)
> >    self.snap.close()
> >    break
>
> > the output data is supposed to read the whole fie but it only reads a
> > part of it. Why is it encountering an early EOF ?
>
> It's not.  In the second case you told it to read only 4096 bytes.  You
> might want to read the docs for the 'read' method, paying particular
> attention to the optional argument and its meaning.
>
> --
> R. David Murray          http://www.bitdance.com

I'm using the above codes in a pthon-fuse's file class's read
function. The offset and length are 0 and 4096 respectively for my
test inputs. When I open a file and read the 4096 bytes from offset,
only a few lines are printed, not the whole file. Actually the file is
only a few bytes. But when I tried reading from the Interactive mode
of python it gave the whole file.

Is there any problem using read() method in fuse-python ?

Also statements like break and continue behaves weirdly in fuse
functions. Any help is appreciated....



More information about the Python-list mailing list