Home  >  Article  >  Backend Development  >  How to use Python to implement the line of code counting tool introduction

How to use Python to implement the line of code counting tool introduction

高洛峰
高洛峰Original
2017-03-16 16:34:011445browse

We often want to count the number of lines of code in a project, but if we want to have a more complete statistical function, it may not be that simple. Today we will take a look at how to use python to implement a line of code statistics tool.

Idea: First get all files, then count the number of lines of code in each file, and finally add the number of lines.

Function implemented:

Count each The number of lines in the file;

Statistics the total number of lines;

Statistics running time;

Supports specifying statistical file types and excludes file types that do not want to be counted;

Recursion Count the number of lines of files in the folder including sub-files;

Exclude empty lines;

# coding=utf-8
import os
import time
basedir = '/root/script'
filelists = []
# 指定想要统计的文件类型
whitelist = ['php', 'py']
#遍历文件, 递归遍历文件夹中的所有
def getFile(basedir):
    global filelists
    for parent,dirnames,filenames in os.walk(basedir):
        #for dirname in dirnames:
        #    getFile(os.path.join(parent,dirname)) #递归
        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
    for file_line in open(fname).xreadlines():
        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)

Result:

[root@pythontab script]# python countCodeLine.py 
/root/script/test/gametest.php---- 16
/root/script/smtp.php---- 284
/root/script/gametest.php---- 16
/root/script/countCodeLine.py---- 33
/root/script/sendmail.php---- 17
/root/script/test/gametest.php---- 16
total lines: 382
Done! Cost Time: 0.00 second
[root@pythontab script]#

Only It is very convenient to count php and python files.

In fact, you can also make improvements on this basis, such as excluding comment lines, etc.

The above is the detailed content of How to use Python to implement the line of code counting tool introduction. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn