Python show-me-the-code No. 0007 Question code statistics

Source: Internet
Author: User

question No. 0007: There is a directory, which is the program you wrote yourself, to count how many lines of code you have written. Include blank lines and comments, but are listed separately.

Idea: Get the directory, and then walk through the directory of code files, counting each file's code, and then finally summarizing the output.

0007. Statistical code. PY

#!/usr/bin/env python#coding: Utf-8ImportOS, re# directory where the code is locatedFile_path ='/home/bill/desktop/github/show-me-the-code ' def analyze_code(codefilesource):    "' opens a py file that counts the number of lines of code in it, including blank lines and comments that return a list with the total number of rows in the file, the number of comment lines, and the number of empty rows "Total_line =0Comment_line =0Blank_line =0     withOpen (Codefilesource) asF:lines = F.readlines () total_line = Len (lines) Line_index =0        # Traverse each row         whileLine_index < Total_line:line = Lines[line_index]# Check if it is a comment            ifLine.startswith ("#"): Comment_line + =1            elifRe.match ("\s* ", line) is  not None: Comment_line + =1                 whileRe.match (". * " ' $ ', line) is None: line = Lines[line_index] Comment_line + =1Line_index + =1            # Check if the line is empty            elifline = ="\ n": Blank_line + =1Line_index + =1    Print "in%s:"% CodefilesourcePrint "Number of lines of code:", Total_linePrint Number of comment lines:, Comment_line,"Occupied%0.2f%%"% (comment_line*100.0/total_line)Print "Blank line number:", Blank_line,"Occupied%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 is locatedOs.chdir (File_path)# traverse the py file in this directoryTotal_Lines =0Total_comment_lines =0Total_blank_lines =0     forIinchOs.listdir (OS.GETCWD ()):ifOs.path.splitext (i) [1] =='. Py ': line = Analyze_code (i) Total_Lines, total_comment_lines, total_blank_lines = Total_Lines + line[0], Total_comment_lines + line[1], Total_blank_lines + line[2]Print "Total lines of code:", Total_LinesPrint total number of comments lines:, Total_comment_lines,"Occupied%0.2f%%"% (total_comment_lines*100.0/total_lines)Print "Total Empty lines:", Total_blank_lines,"Occupied%0.2f%%"% (total_blank_lines*100.0/total_lines)if__name__ = =' __main__ ': Run (File_path)

Effect:

Python show-me-the-code No. 0007 Question 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.