Normal files throw exceptions if you mix methods. >>> f = open("words") >>> for l in f: ... break ... >>> f.tell() 8192L >>> f.readline() Traceback (most recent call last): File "", line 1, in ValueError: Mixing iteration and read methods would lose data BZ2Files silently do the wrong thing. (Output is a coincidence. Honest!) >>> import bz2 >>> f = bz2.BZ2File("words.bz2") >>> for l in f: ... break ... >>> f.tell() 8192L >>> f.readline() 'lose\n' Expected behaviour is for it to throw a ValueError like normal file objects.
Well I'm not the bz2 maintainer but I could take a look. The BZ2File implementation is generally a straight ripoff of the 2.x file object, with (de)compression calls added where necessary. I guess the ripoff was a one-shot effort and subsequent maintenance wasn't done. It makes it quite a bit alien in the 3.x IO landscape, but until someone decides to rewrite it we'll have to live with that.