-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountingCodeLines.py
34 lines (34 loc) · 1.1 KB
/
CountingCodeLines.py
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
28
29
30
31
32
33
34
# coding=utf-8
import os
import time
# 设定根目录
basedir = './'
filelists = []
# 指定想要统计的文件类型
whitelist = ['cpp', 'h']
#遍历文件, 递归遍历文件夹中的所有
def getFile(basedir):
global filelists
for parent,dirnames,filenames in os.walk(basedir):
for filename in filenames:
ext = filename.split('.')[-1]
#只统计指定的文件类型,略过一些log和cache文件
if ext in whitelist:
filelists.append(os.path.join(parent,filename))
#统计一个的行数
def countLine(fname):
count = 0
# 把文件做二进制看待,read.
for file_line in open(fname, 'rb').readlines():
if file_line != '' and file_line != '\n': #过滤掉空行
count += 1
print (fname + '----' , count)
return count
if __name__ == '__main__' :
startTime = time.clock()
getFile(basedir)
totalline = 0
for filelist in filelists:
totalline = totalline + countLine(filelist)
print ('total lines:',totalline)
print ('Done! Cost Time: %0.2f second' % (time.clock() - startTime))