SagerXiao's Blog

Restart of an old programmer

Python计算文件行数的三种方法

Python计算文件行数的三种方法

Caculate file lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import timeit,os
def linecount_1():
    return len(open('data.sql').readlines())    #最直接的方法

def linecount_2():
    count = -1 #让空文件的行号显示0
    for count,line in enumerate(open('data.sql')): pass
    #enumerate格式化成了元组,count就是行号,因为从0开始要+1
    return count+1
 
def linecount_3():
    count = 0
    thefile = open('data.sql','rb')
    while 1:
        buffer = thefile.read(65536)
        if not buffer:break
        count += buffer.count('\n') #通过读取换行符计算
    return count
 
for f in linecount_1,linecount_2,linecount_3:
    print f.__name__,f()
     
    for f in linecount_1,linecount_2,linecount_3:
        #第一个参数是函数,第二个参数是环境,例如import模块
        t = timeit.Timer(f,'')
        #第一个参数是重复整个测试的次数,第二个参数是每个测试中调用被计时语句的次数。
        print min(t.repeat(100,3))

结果:

linecount_1 43119

linecount_2 43119

linecount_3 43119

0.04646344717

0.0403758019525

0.0126613857348

Comments