Python show-me-the-code No. 0007 Problem statistic code lines (comment, blank line, total number of rows) applet

Source: Internet
Author: User

No. 0007: There is a directory, which is your own written procedures, statistics on how many lines of code you have written. Include blank lines and comments, but be listed separately.

Train of thought: Get the directory, then traverse the directory of the code files, one by one to count each file code, and then the final summary of output.

0007. Statistics code. PY

#!/usr/bin/env python #coding: utf-8 import OS, re # code directory File_path = '/home/bill/desktop/github/show-me-the-code ' def 
    Analyze_code (Codefilesource): "Open a py file that counts the number of lines of code in it, including blank lines and comments returns a list of the total number of rows, comment lines, blank lines ' Total_line = 0 Comment_line = 0 Blank_line = 0 with open (codefilesource) as F:lines = F.readlines () total_l ine = Len (lines) Line_index = 0 # Traverse each row while Line_index < Total_line:line = lines  [Line_index] # Check for Comments if Line.startswith ("#"): Comment_line + 1 Elif  Re.match ("\s*" ", line) are not none:comment_line + = 1 while Re.match (". * "" $ ", line) is None:line = Lines[line_index] Comment_line + = 1 Line_index + = 1 # Check for blank lines elif line = = "\ n": Blank_line + + 1 Line_index + + 1 p Rint "in%s:"% codefilesOurce print "Lines of code:", total_line print "comment lines:", Comment_line, "account for%0.2f%%"% (comment_line*100.0/total_line) prin  T "Number of empty lines:", Blank_line, "%0.2f%%"% (blank_line*100.0/total_line) return [Total_line, Comment_line, Blank_line] def  Run (file_path): # Switch to the directory where code resides Os.chdir (file_path) # traverse the py file in the directory total_lines = 0 Total_comment_lines =
            0 total_blank_lines = 0 for I in Os.listdir (OS.GETCWD ()): If Os.path.splitext (i) [1] = = '. Py ': line = Analyze_code (i) Total_Lines, total_comment_lines, total_blank_lines = Total_Lines + line[0], total_com  Ment_lines + line[1], Total_blank_lines + line[2] print "Total lines of code:", total_lines print "Total comment lines:", Total_comment_lines, "%0.2f%%"% (total_comment_lines*100.0/total_lines) print "Total empty lines:", Total_blank_lines, "account for%0.2f%%"% (total_blank_li
 Nes*100.0/total_lines) If __name__ = = ' __main__ ': Run (file_path)

Effect:

Related Article

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.