小文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#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)
|
大文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#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 ***
本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1346770如需转载请自行联系原作者
RQSLT