This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Odd behavior in the file object
Type: Stage:
Components: Library (Lib) Versions: Python 2.2
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: davidnemeth, terry.reedy
Priority: normal Keywords:

Created on 2003-07-09 20:05 by davidnemeth, last changed 2022-04-10 16:09 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bug.zip davidnemeth, 2003-07-09 20:05 archive of example code and data file showing problem
Messages (4)
msg17014 - (view) Author: David Nemeth (davidnemeth) Date: 2003-07-09 20:05
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
msg17015 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2003-07-13 21:32
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.
msg17016 - (view) Author: David Nemeth (davidnemeth) Date: 2003-07-14 12:54
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.
msg17017 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2003-07-14 16:54
Logged In: YES 
user_id=593130

Agreed.  I recommend you check behavior of 2.3 (due by end 
of month - now in final bug fix stage) and raise issue on 
comp.lang.python if not satifactorily changed.
History
Date User Action Args
2022-04-10 16:09:54adminsetgithub: 38830
2003-07-09 20:05:17davidnemethcreate