Python code example for calculating the number of lines of code

Source: Internet
Author: User
Tags webhost
This article mainly introduces python statistics on the number of lines of code simple examples of relevant information, you can refer to the next article to introduce python statistics on the number of lines of code simple examples of relevant information, for more information, see

Python simple example of counting code lines

When sending the test, we found that we needed to count the number of lines of code.

So I wrote a small program to count the number of lines of my code.


#calclate_code_lines.py import os  def afileline(f_path):   res = 0   f = open(f_path)   for lines in f:     if lines.split():       res += 1   return res  if name=='main':   host = 'E:'+os.sep+'develop'+os.sep+'dev_workspace'+os.sep+'AptanaStudio3'+os.sep+'webhost'      allfiles = 0   allline = 0        for root,dirs,files in os.walk(host):     for afile in files:              if(root.startswith(host+os.sep+'entries')):         continue       elif(root.startswith(host+os.sep+'.svn')):         continue       elif(root.startswith(host+os.sep+'.settings')):         continue       elif(root.startswith(host+os.sep+'logs')):         continue       elif(root.startswith(host+os.sep+'static')):         continue        elif(root.startswith(host+os.sep+'payload'+os.sep+'.svn')):         continue       elif(root.startswith(host+os.sep+'dist'+os.sep+'.svn')):         continue       elif(root.startswith(host+os.sep+'dsync'+os.sep+'.svn')):         continue       elif(root.startswith(host+os.sep+'hcache'+os.sep+'.svn')):         continue       elif(root.startswith(host+os.sep+'test'+os.sep+'.svn')):         continue       elif(root.startswith(host+os.sep+'webhost'+os.sep+'.svn')):         continue       elif(root.startswith(host+os.sep+'wsgi'+os.sep+'.svn')):         continue       elif(root.startswith(host+os.sep+'hcache'+os.sep+'templates'+os.sep+'.svn')):         continue       elif(root.startswith(host+os.sep+'dsync'+os.sep+'hcache'+os.sep+'.svn')):         continue       else:          ext = afile.split('.')         ext = ext[-1]         if (ext in ['py','css','js','html','txt','docx','wsgi']):           itpath = root+os.sep+afile           allfiles += 1           allline +=afileline(itpath)           print (root+os.sep+afile)              print ('Total: ',allfiles)   print ('Total lines:',allline)

Later, we can make it easier for future code statistics.

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.