Welcome to the Csdn-markdown Editor

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.