readline in while loop

Anton Muhin antonmuhin at sendmail.ru
Fri May 23 08:31:51 EDT 2003


Geert Fannes wrote:
> hello, i tried to construct a program to read a file, line after line. i 
> have no idea why it does not work...
> 
> f=file("test.txt","r");
> while l=f.readline():
>     print l;
> 
> the problem is with "while l=f.readline():" the next program does work
> 
> f=file("test.txt","r");
> while f.readline():
> 
> i tried things like
> 
> f=file("test.txt","r");
> while (l=f.readline())!="":
>     print l;
> 
> but that doesn't work either. just reading the lines is not enough, i 
> want to do something with the read lines.
> 
> geert.
> 
Let me guess --- you are coming from C/C++?

Python's idiom might be:

f = file("test.txt", 'r')
for line in f:
	print line
f.close()

hth,
anton.





More information about the Python-list mailing list