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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM