统计某个目录文件下python的有效代码总行数及单个文件的代码量
import os
import sys
import os.path
#for i in sys.argv:
# print(i)
"""
根据命令行输入的统计目录及文件类型进行统计目录或单个文件的代码行数
"""
#统计单个文件代码量
def count_file_lines(file_path):
#记录单个文件代码量
line_count = 0
#用于标记是否要记录多行注释代码行,True代码
flag = True
#获取编码格式
try:
fp = open(file_path,"r",encoding="utf-8")
encoding_type="utf-8"
except:
encoding_type="gbk"
finally:
fp.close()
#打开文件统计代码
with open(file_path,"r",encoding=encoding_type) as fp:
for line in fp:
#去除每行两端的空白
if line.strip()=="":
continue
else:
#如果以")"结尾且falg==False,需要把flag置为True,统计本行
if line.strip().endswith(')') and flag == False:
flag=True
line_count+=1
continue
#如果以三引号结尾且falg==False,需要把flag置为True,证明跳出多行注释了
#同时跳过本行,不统计
if line.strip().endswith("'''") and flag == False:
flag=True
continue
if line.strip