This article mainly introduces how to use Python scripts to share code statistics on the number of lines of code. This article provides implementation code, usage methods, and statistical results, need a Friend can refer to the previous implementation of bash (http://www.bitsCN.com/article/61943.htm), but that can not be used in windows, so I wrote a python version, it is also convenient for me to use ...... I will not talk about it here, but google doesn't understand it.
Implementation code
The code is as follows:
#! /Usr/bin/python
'''
File: count. py
Author: Mike
E-mail: Mike_Zhang@live.com
'''
Import sys, OS
Extens = [". c", ". cpp", ". hpp", ". h"]
LinesCount = 0
FilesCount = 0
Def funCount (dirName ):
Global extens, linesCount, filesCount
For root, dirs, fileNames in OS. walk (dirName ):
For f in fileNames:
Fname = OS. path. join (root, f)
Try:
Ext = f [f. rindex ('.'):]
If (extens. count (ext)> 0 ):
Print 'support'
FilesCount + = 1
Print fname
Rochelle count = len (open (fname). readlines ())
Print fname, ":", l_count
LinesCount + = l_count
Else:
Print ext, ": not support"
Except t:
Print "Error occur! "
Pass
If len (sys. argv)> 1:
For m_dir in sys. argv [1:]:
Print m_dir
FunCount (m_dir)
Else:
FunCount (".")
Print "files count:", filesCount
Print "lines count:", linesCount
Raw_input ("Press Enter to continue ")
Usage
1. for this directory
The code is as follows:
./Count. py
2. count multiple directories
The code is as follows:
./Count. py/tmp ~
Running effect
Okay, that's all. I hope it will help you.