python中如何統計文件的行數


1、測試數據

root@PC1:/home/test# ls
test.py  test.txt
root@PC1:/home/test# cat test.txt   ## 測試數據
w r s f
z s d g
z c w d
z d g g
z c e w
root@PC1:/home/test# cat test.py
fp = open("test.txt", "r")
length = len(fp.readlines()) ## 將讀入文件轉化為列表, 然后統計列表的長度
print("length = ", length)       ## 打印結果

fp.close()
root@PC1:/home/test# python3 test.py
length =  5

 

2、

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM