读取功能可一次读取整个文件。您可以使用readlines函数逐行读取文件。
您可以使用以下内容逐行读取文件:
f = open('my_file.txt', 'r+') for line in f.readlines(): print line f.close()
您也可以使用with ... open语句打开文件并逐行读取。
with open('my_file.txt', 'r+') as f: for line in f.readlines(): print line
https://www.codespeedy.com/read-a-specific-line-from-a-text-file-in-python/