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、

原文地址:https://www.cnblogs.com/liujiaxin2018/p/15706080.html