Python 文件行数读取的三种方法

Python三种文件行数读取的方法:

#文件比较小
count = len(open(r"d:\lines_test.txt",'rU').readlines())
print count

#文件比较大
count = -1

for count,line in enumerate(open(r"d:\lines_test.txt",'rU')):
pass
count += 1

print count

#更好的方法
count = 0

thefile = open(r"d:\lines_test.txt",'rb')

while True:
buffer = thefile.read(1024 * 8192)
if not buffer:
break
count += buffer.count('\n')
thefile.close()

print count

猜你喜欢

转载自www.cnblogs.com/xiaoniu-666/p/10386067.html
今日推荐