[Python-bugs-list] [ python-Bugs-768698 ] Odd behavior in the file object

SourceForge.net noreply@sourceforge.net
Mon, 14 Jul 2003 05:54:13 -0700


Bugs item #768698, was opened at 2003-07-09 20:05
Message generated for change (Comment added) made by davidnemeth
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=768698&group_id=5470

Category: Python Library
Group: Python 2.2.2
>Status: Closed
Resolution: None
Priority: 5
Submitted By: David Nemeth (davidnemeth)
Assigned to: Nobody/Anonymous (nobody)
Summary: Odd behavior in the file object

Initial Comment:
Hello,
  I was using
Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit
(Intel)] on win32
to write a file to parse a text file.  The file was
divided in two parts, so I wrote something like this:

file = open("foobar" ,"r")
for line in file:
    #read a few lines
     break

for line in file:
     read rest of lines

A chunk of the middle part of the file ends up missing.
 Oddly, it's not the section just after the first few
lines are read.

I've attached an example script and data file which
shows this problem on my system (Windows 2000).  

The workaround I'm using (which works) is :
file = open("foobar","r")
while 1:
    line  = file.readline()
    if line is what I want:
         break
for line in file:
     do stuff

Which seems to work


----------------------------------------------------------------------

>Comment By: David Nemeth (davidnemeth)
Date: 2003-07-14 12:54

Message:
Logged In: YES 
user_id=819391

I have closed this item, but it would be nice to see the
Python behave in a less surprising fashion in future releases.

----------------------------------------------------------------------

Comment By: Terry J. Reedy (tjreedy)
Date: 2003-07-13 21:32

Message:
Logged In: YES 
user_id=593130

I believe that the file and iteration specs leave reiteration 
behavior undefined, making this not-a-bug, even if 
annoying.  In any case, this is known behavior.

Explanation (from c.l.py postings): a 2.2 file iterator 
(apparently a separate object from file object itself) reads 
blocks of the file and then yields a line at a time.  When you 
break, leftover read data in the last block is discarded.

In 2.3, I believe file object is its own iterator.  Don't know if 
it fixed this wart.  In any case, perhaps you could close this.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=768698&group_id=5470