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

Sreejith K sreejithemk at gmail.com
Sat Mar 21 02:30:03 EDT 2009


On Mar 21, 10:54 am, "R. David Murray" <rdmur... at bitdance.com> wrote:
> Sreejith K <sreejith... at gmail.com> wrote:
> >                                    tf.writelines("Reading from Base File\n")
> >                                    self.file.seek(block*4096 + off%4096)
> >                                    bend = 4096-(off%4096)
> >                                    if length-bend <= 0: ## if only a part of a block is to be read
> > (not till the end of the block)
> >                                            data = self.file.read(length)
> >                                            break
> >                                    data += self.file.read(bend)
> >                                    length -= bend
> >                                    off = 4096
> >                            return data
>
> > This is the filesystem class for files. Whenever a read occurs an
> > instance is created and read function is called. In my example when
> > accessing a file named 'mango.txt' it checks for mango.txt_snaps/snap1
> > dirctory and open file '0' as self.snap. But the read() returns (i.e
> > data) a small part....
>
> > Almost all the code worked weird in this example. Apart from read(),
> > the break and continue also works weird. When opening the file
> > 'mango.txt' the following output is written by tf (an output file).
> > Here METHOD is not NORMAL, self.snap_cnt is 1, blocks is [['0']]
>
> > File initiating..
> > File initiated..
> > Read length: 4096 offset: 0
> > Snapshot 0 opened..
> > Snap read
> > Partial read from snap
> > Snapshot 0 opened..
> > Block not in snap
> > Reading from Base File
>
> > See the weirdness of continue and break here ?(loop was supposed to
> > loop only once as rev_snap_list contains 0 only)
>
> I'm not going to look at all this code now...it looks way too complicated
> and in need of some serious refactoring :)
>
> But a couple of on-point comments:
>
> How do you know rev_snap_list contains only 0?  You didn't log it.
>
> Same for the read.  How do you know the read didn't read the whole
> file?  You didn't log it.
>
> Both your statements might be true, but until you show the logging
> output proving it, you don't _know_ that your assumptions are true.
>
> --
> R. David Murray          http://www.bitdance.com

Thanks for your comments.

As I said, self.snap_cnt is 1 then

>>> snap_list = range(1)
>>> rev_snap_list = snap_list
>>> rev_snap_list
[0]
>>> rev_snap_list.reverse()
>>> rev_snap_list
[0]

So I thought there is no need to log it. In the above code, as break
and continue doesn't work as I expected the read() is called twice
("Partial read from snap" and "reading from base file") the output is
a combination of the 2 reads (so it can't be read using 'less'). But
the expected result was "Partial read from snap" i.e read(4096) and
break from the loop. Instead after this break, the loop is again
started ("Snapshot 0 opened.. "). I have no idea what went wrong here.
But when I comment out block_read = False before the continue
statement the log displays the following and the file is read
partially....

File initiating..
File initiated..
Read length: 4096 offset: 0
Snapshot 0 opened..
Snap read
Partial read from snap
Snapshot 0 opened..
Block not in snap



More information about the Python-list mailing list