Counting lines of code with Python for getting started with Python

Pycharm writes a lot of code every day, how to count the number of lines of code per day? As a coder with a target of 100,000 lines, think of a way!

 

Topic: There is a directory, which is the program you wrote yourself, count how many lines of code you have written. Include blank lines and comments, but list them separately. 

 

First of all, let’s analyze the idea and take a look at the steps of installing an elephant in a refrigerator. From a given directory, it takes roughly the following 7 steps to count all the lines of code in the directory:

1. Traverse all the files in the directory.

2. Determine whether the file ends with ".py". (Take python code as an example)

3. Open the .py file (do not use W+, W+ will clear the file content)

4. Loop through each line of the file

5. Determine the content of each line:

                  (a) Notes: Start with #.

                  (b) Comments: End with triple quotation marks.

                  (c) Blank line: nothing but blank characters.

                  (d) Code line: There are other characters left after the blank character.

6. Determine whether it is the end of the file,    

7. Close the file and return the result. 

 

After the problem-solving idea is clear, the rest is to build the code of each module like building blocks and it's done (the sample code is at the end of this article): 

 

1: Import the OS, define the code_lines_count function and receive a path formal parameter, declare three variables for counting code lines, comment lines and blank lines respectively 

 

2: Traverse the file object obtained by os.walk, then use the splitext function to split the suffix name of the file, and then use the list index [1] to obtain the file suffix name, and determine whether it ends with ".py". (listdir can also be used here, but listdir can only take files in a single-layer directory, and it is more troublesome to determine whether the obtained element is a file or a folder separately)

3: A variable file_abs_path is defined and the absolute path of the file is assigned, because the following code will be used multiple times, so it is not necessary to use so.path.join(xx,xx) every time. 

4: Use the with method to open the file ending with ".py" obtained in the previous step (using with can save the code for closing the file), and use the While True loop for the opened file to read the file using readline() each line and assign it to the line variable. 

5: This code is used to judge whether the line obtained by readline() in the previous step is a code line, a blank line or a comment line.

         a: If the line is empty, it means that the end of the file is fetched. At this time, the break while loop will continue to the next file operation in files. 

         b: Use strip() to remove blanks from the line obtained by readline(). If it starts with "#" after processing, it means that this line is a comment line. At this time, add 1 to comm_lines. 

         c: Use strip() to de-blank the line obtained by readline(). If it starts with three single quotes or three double quotes after processing, it means that this is the beginning of a multi-line comment, and then judge the line If the number of triple quotes is 1, it means that the comment is divided into multiple lines, otherwise the comment is one line (a pair of triple quotes is on the same line). For the case where the comment is multiple lines, use a while loop to read the next line, and no line is read. comm_lines is added by 1. If there are triple quotes in a line, it is judged that the comment is over, and the break is a while loop (only the more standardized comments are considered here) 

         d: If the read line is non-empty and not a comment after strip(), it is a code line, and add 1 to code_lines.

         e: If none of the above conditions are satisfied, it is judged as a blank line, and 1 is added to space_lines. 

6: Return the counted lines of code, comment lines and blank lines.

7: Test code The following figure is an example of running

 

import os


def code_lines_count(path):
    code_lines = 0
    comm_lines = 0
    space_lines = 0
    for root,dirs,files in os.walk(path):
        for item in files:
            file_abs_path = os.path.join(root,item)
            postfix = os.path.splitext(file_abs_path)[1]
            if postfix == '.py':


                with open(file_abs_path) as fp:
                    while True:
                        line = fp.readline()
                        if not line:

                            break
                        elif line.strip().startswith('#'):

                            comm_lines += 1
                        elif line.strip().startswith("'''") or line.strip().startswith('"""'):
                            comm_lines += 1
                            if line.count('"""') ==1 or line.count("'''") ==1:
                                while True:
                                    line = fp.readline()

                                    comm_lines += 1
                                    if ("'''" in line) or ('"""' in line):
                                        break
                        elif line.strip():

                            code_lines += 1
                        else:

                            space_lines +=1

    return code_lines,comm_lines,space_lines

if __name__ == '__main__':
    abs_dir = os.getcwd()
    x,y,z = code_lines_count(abs_dir)
    print(x,y,z)

 

 

reference

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325205663&siteId=291194637