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