new linereading standard?

Mike Fletcher mfletch at tpresence.com
Tue Apr 25 12:55:57 EDT 2000


Or, if you prefer...

import fileinput # standard module
f = fileinput.FileInput( filename )
for line in f:
	print line

Enjoy,
Mike

-----Original Message-----
From: dyoo [mailto:dyoo at uclink4.berkeley.edu]
Sent: Tuesday, April 25, 2000 1:04 PM
To: python-list at python.org
Subject: Re: new linereading standard?


In article <3904D21B.333DD930 at visionart.com>, 
Pete Shinners <pete at visionart.com> wrote:
> i've come to the point where i can't get by even the simplest python
> project without dumping this code into my files. it is a simple class
> that handles the "while 1: .... break" carnival for reading files.
> now i can code in a preferred style

> myfile = open('myfile.txt', 'r')
> for line in filelines(myfile):
>     print line
> myfile.close()

> as a person trying to introduce his friends to python, i repeatedly find
> this is the ugliest wart while doing basic stuff with python.

Python does allow you to read the whole file at a time.  
readlines() will give you a list of all the lines in the file, so 
your code can look like:

  myfile = open('myfile.txt', 'r')
  for line in myfile.readlines():
    print line
  myfile.close()

-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list