python 获取文件行数

#如果要统计文件的行数,可以这样写:
count = len(open(filepath, 'r').readlines())
#这种方法简单,但是可能比较慢,当文件比较大时甚至不能工作。

#可以利用enumerate():
count = 0
for index, line in enumerate(open(filepath,'r')): 
    count += 1

#可以利用readlines()
count=0
f = open("filepath","r")
for line in f.readlines():
count=count+1
 

猜你喜欢

转载自www.cnblogs.com/mghhzAnne/p/10208067.html
今日推荐