file.read() doesn't read the whole file
R. David Murray
rdmurray at bitdance.com
Fri Mar 20 12:43:44 CET 2009
Sreejith K <sreejithemk at gmail.com> wrote:
> >>> 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()
> >>> snapdir + '/snap%s/%s' % (repr(snap_cnt), repr(block))
> 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)
> 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
More information about the Python-list