Recently the company applied for copyright, need to count the number of lines of code, using the Internet to find the tools, but also to install what, feel not reliable, wrote a python script to statistics
1. Principle
It's simple to get all the files and then count the lines of code in each file and add the number of rows.
ImportOsImportOs.pathImportTimerootdir =' file path 'Filelists = []#遍历文件 def getFile(rootdir): GlobalFilelists forParent,dirnames,filenamesinchOs.walk (RootDir): forDirNameinchDirnames:getfile (Os.path.join (parent,dirname))#嵌套 forFileNameinchFilenames#print "filename is:" + filenameFilelists.append (Os.path.join (parent,filename))#统计一个文件的行数 def countline(fname):Count =0 forFile_lineinchOpen (fname). Xreadlines ():ifFile_line! ="' andFile_line! =' \ n ':#过滤掉空行Count + =1 PrintFName +' ... ', CountreturnCountif__name__ = =' __main__ ': StartTime = Time.clock () getFile (rootdir) Totalline =0 forFileListinchFilelists:totalline = Totalline + countline (filelist)Print ' totalline: ', TotallinePrint ' done:%0.2f second '% (Time.clock ()-StartTime)
2. Results
Average efficiency, ran more than 10,000 files, spent 1.5
Welcome to the Csdn-markdown Editor