# -*- encoding=utf8 -*- # Created Date: 2021/10/11 # @Author: "jiliqian" import re, os code_file_source = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lianxi1.py') def get_lines(): if '.py' in code_file_source: comment_line = 0 blank_line = 0 multi_comment_line = 0 with open(code_file_source,encoding='utf-8', mode='r+') as f: lines = f.readlines() line_index = 0 if len(lines) == 0: print("此.py文件代码内容为空") else: while line_index < len(lines): line = lines[line_index] if line.startswith("#"): comment_line += 1 # 检查是否为#注释 elif line == '\n': blank_line += 1 # 检查是否为空行 elif line.startswith("'''"): multi_comment_line = +1 #检查是否为多行注释 line_index += 1 lines_after_alter = int(len(lines)) - int(comment_line) - int(blank_line) - int(multi_comment_line) print(lines_after_alter) # 输出python文件代码行数 else: print("此文件夹下无.py文件") if __name__=='__main__': get_lines()
08-02
4163
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)