Python script implements code row count statistics for code sharing, and python row count
Previously implemented with bash (http://www.bkjia.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
Copy codeThe 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
Copy codeThe Code is as follows:
./Count. py
2. Count multiple directories
Copy codeThe Code is as follows:
./Count. py/tmp ~
Running Effect
Okay, that's all. I hope it will help you.