Python如何统计行数
发布时间:2026-06-07 21:59:09

Python如何统计行数?
方法一:
count=len(open(filepath,'r').readlines())
这种方法很简单,但可能很慢,当文件相对较大时甚至不能工作。
方法二:
enumerate()可用于统计文件函数:
count=0 forindex,lineinenumerate(open(filepath,'r')): count+=1 printcount
Python教程推荐学习。
下一篇 Python怎么改图片大小
