python 脚本 - 计算脚本中的代码行数

python script - counting lines of code in the script

我是 运行 我的 raspberry pi 上的一个 python 脚本,我只是想知道是否有任何我可以使用的命令来计算我的脚本中有多少行。

此致

您可以 get the path of the script being run 然后读取文件,计算所有既不为空也不以 # 符号或 '''""" 开头的行(全部其中表示评论):

#!/usr/bin/env python3

'''
This script prints out all significant lines of code contained within 
itself.
'''

import os
import re

SINGLE_LINE_COMMENT_DELIMITER = "#"
MULTILINE_COMMENT_DELIMITER_PATTERN = re.compile("['\"]{3}")

class SignficantLineParser(object):
def __init__(self):
    self.in_comment_section = False

def parse(self, line):
    line = line.strip()
    if self.in_comment_section:
        if line.startswith(SINGLE_LINE_COMMENT_DELIMITER):
            return False
        else:
            if MULTILINE_COMMENT_DELIMITER_PATTERN.match(line):
                # Exiting multi-line comment
                self.in_comment_section = False             
    elif line:
        if line.startswith(SINGLE_LINE_COMMENT_DELIMITER):
            return False
        else:
            if MULTILINE_COMMENT_DELIMITER_PATTERN.match(line):
                # Entering multi-line comment
                self.in_comment_section = True
                return False
            else:
                return True
    else:
        return False


script_path = os.path.realpath(__file__)
with open(script_path, 'r') as inf:
    parser = SignficantLineParser()
    significant_lines = 0
    for line in inf:
        if parser.parse(line):
            significant_lines += 1
            print("Significant line: " + line, end="")

print("\n\nSignificant line count: %d" % significant_lines)

打印出来:

Significant line: import os
Significant line: import re
Significant line: SINGLE_LINE_COMMENT_DELIMITER = "#"
Significant line: MULTILINE_COMMENT_DELIMITER_PATTERN = re.compile("['\"]{3}")
Significant line: class SignficantLineParser(object):
Significant line:   def __init__(self):
Significant line:       self.in_comment_section = False
Significant line:   def parse(self, line):
Significant line:       line = line.strip()
Significant line:       if self.in_comment_section:
Significant line:           if line.startswith(SINGLE_LINE_COMMENT_DELIMITER):
Significant line:               return False
Significant line:           else:
Significant line:               if MULTILINE_COMMENT_DELIMITER_PATTERN.match(line):
Significant line:                   self.in_comment_section = False             
Significant line:       elif line:
Significant line:           if line.startswith(SINGLE_LINE_COMMENT_DELIMITER):
Significant line:               return False
Significant line:           else:
Significant line:               if MULTILINE_COMMENT_DELIMITER_PATTERN.match(line):
Significant line:                   self.in_comment_section = True
Significant line:                   return False
Significant line:               else:
Significant line:                   return True
Significant line:       else:
Significant line:           return False
Significant line: script_path = os.path.realpath(__file__)
Significant line: with open(script_path, 'r') as inf:
Significant line:   parser = SignficantLineParser()
Significant line:   significant_lines = 0
Significant line:   for line in inf:
Significant line:           if parser.parse(line):
Significant line:               significant_lines += 1
Significant line:               print("Significant line: " + line, end="")
Significant line: print("\n\nSignificant line count: %d" % significant_lines)

Significant line count: 35

您可以使用wc命令:

wc -l yourScript.py