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

R. David Murray rdmurray at bitdance.com
Fri Mar 20 16:20:42 CET 2009


On Fri, 20 Mar 2009 at 07:09, Sreejith K wrote:
> 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 wholefiletill EOF. But
>>> when this method is used in a different scenario thefileis notread
>>> completely. I'll post the code thatreadonly some part of thefile...
>>
>>> 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 toreadthe 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 toreadonly 4096 bytes.  You
>> might want toreadthe docs for the 'read' method, paying particular
>> attention to the optional argument and its meaning.
>>
>> --
>> R. David Murray          http://www.bitdance.com
>
> Thanks for the reply,
> Actually the file is only few bytes and file.read() and file.read
> (4096) will give the same result, i.e the whole file. But in my case
> its not happening (using it in python-fuse file class).. Any other
> ideas ?

Not offhand.  If it were me I'd start playing with parameters and moving
things around, trying to find additional clues.  See if calling read
without the argument in the second case works, start stripping the second
case down until it starts working (even if wrongly for the ultimate goal),
and things like that.

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


More information about the Python-list mailing list