while true: !!!

Carsten Gaebler cg at schlund.de
Mon Dec 11 08:02:00 EST 2000


Jaroslav Gergic wrote:

> Do you know better construction to avoid both - duplicate lines
> and horrible 'while 1' construction?
> 
> Something like:
> 
> while( "" != (line = fh.readline()) ):
>   ... do something ...

You could write a wrapper class like this:

class MyFile:
	def __init__(self, filename):
		self.f = open(filename, "r")

	def readline(self):
		self.line = self.f.readline()
		return self.line


And then something like:

F = MyFile("somefile")
while F.readline():
	print F.line


cg.



More information about the Python-list mailing list