"for line in fd" twice

Damien Metzler dmetler.nospam at partner.auchan.fr
Tue Jun 3 17:58:54 CEST 2003


Thomas Güttler wrote:
> Hi!
> 
> the following does not work:
> 
> """
> fd=open(file)
> for line in fd:
>     if line.startswith("mymark"):
>         break
> for line in fd:
>     #Read lines after "mymark"
>     ....
> fd.close()
> """
> 
> The second loop misses some lines.
> 
> It works with the old way:
> 
> """
> while 1:
>     line=fd.readline()
>     if not line:
>         break
> """
> 
> instead of "for line in fd"
> 


after u do a :

for line in fd:
    #put ur code here
    ...

u don't know in what state fd is after that. Perhaps the implementation 
says it goes till the end, or till next record....

boTreatLine=false
for line in fd :
    if line.startswith("mymark"):
         boTreatLine=true
    if boTreatLine=true:
         #Read lines after mymark

> If you mustnot call "for line in fd" twice, it would 
> be nice to get an exception which reminds me about it.
> 

This is specific to the generator implementation and this is a "logic" 
feature, not an exception

> Version:
>  Python 2.2.2 (#1, Mar 17 2003, 15:17:58)
>  [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
> 
>  thomas
> 








More information about the Python-list mailing list