pythonian way
Fredrik Lundh
effbot at telia.com
Fri Feb 25 03:13:13 EST 2000
Milos Prudek <prudek at nembv.cz> wrote:
> The following excerpt works. It reads lines from config file. But I
> would like to ask if it is 'right' in the Pythonian way. Since I'm just
> beginning in Python I do not want to twist Python into 'my' way of
> thinking.
>
> There is no eof() function for high level readline(), so I use '' to
> discover end of file.
>
> A=[]
> Line=H.readline()
> while Line<>'':
> if Line<>'' and Line<>'\n':
> A.append(Line)
> Line=H.readline()
if all you want is to load all lines from the file
into a list, use:
A = H.readlines()
the standard pydiom for reading from a text file
is this:
while 1:
s = fp.readline()
if not s:
break
# do something to s
(it's used everywhere, so you better get used
to it! "while 1" should be read as "forever")
also see the "fileinput" module.
</F>
More information about the Python-list
mailing list