以行的形式读出一个文件最简单的方式是使用文件对象的readline()、readlines()和xreadlines()方法。

Python2.2+为这种频繁的操作提供了一个简化的语法——让文件对象自身在行上高效迭代(这种迭代是严格的向前的)。
为了读取整个文件,可能要使用read()方法,且使用字符串的split()来将它拆分WEIGHT行或其他块。
下面是一些例子:
>>> for line in open('chap1.txt'): # Python 2.2+
... # process each line in some manner
... pass
...
>>> linelist = open('chap1.txt').readlines()
>>> print linelist[1849],
EXERCISE: Working with lines from a large file
>>> txt = open('chap1.txt').read()
>>> from os import linesep
>>> linelist2 = txt.split(linesep)