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