new linereading standard?

dyoo dyoo at uclink4.berkeley.edu
Tue Apr 25 13:03:55 EDT 2000


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()




More information about the Python-list mailing list