first, second, etc line of text file

Jeff jeffober at gmail.com
Wed Jul 25 22:00:28 CEST 2007


Files should be iterable on their own:

filehandle = open('/path/to/foo.txt')
for line in filehandle:
    # do something...

But you could also do a generic lines = filehandle.readlines(), which
returns a list of all lines in the file, but that's a bit memory
hungry.




More information about the Python-list mailing list