Reading a whole text file at once: readlines()

readlines()

  • returns a list

  • each element is a string with the content of one line from the file

The text file file.txt:

This is the first line.
This is the second line.

is read by:

aFile = file("file.txt", "r")·

aList = aFile.readlines()      # read the file

aFile.close()

print aList
print aList[1]

printing:

['This is the first line. \n', 'This is the second line.\n']
This is the second line.