Python按行读取文件

小文件:

#coding=utf-8
#author: walker
#date: 2013-12-30
#function: 按行读取小文件
all_lines = []
try:
    file = open('txt.txt', 'r')
    all_lines = file.readlines()
except IOError as err:
    print('File error: ' + str(err))
finally:
    if 'file' in locals():
        file.close()
for line in all_lines:
    print(line)

大文件:

#coding=utf-8
#author: walker
#date: 2013-12-30
#function: 按行读取大文件
try:
    file = open('txt.txt', 'r')
    for line in file:
        print(line)
except IOError as err:
    print('File error: ' + str(err))
finally:
    if 'file' in locals():
        file.close()


相关阅读:Python开关文件的两种方式


*** walker ***


你可能感兴趣的:(python,readlines,按行读取文件)