python脚本-简单读取有效python代码量

import os
count=[0,0]
paths=[]
file_count=[0]
def sum_code(path):
    if os.path.isfile(path):
        one_file(path)
    else:
        paths=os.walk(path)
        for i in paths:
            for j in i[2]:
                path=i[0]+"\\"+j
                one_file(path)
def one_file(file_path):
    if file_path[-3:]==".py":
        file_count[0]+=1
        tag=True
        with open(file_path,"r",encoding="utf-8") as file:
            for line in file:
                if line.startswith("\'\'\'")or line.startswith('\"\"\"'):
                    tag=not tag
                    continue
                elif (not line.startswith("#") and  len(line)) and tag==True:
                    count[0]+=1
                    count[1]+=1
                else:
                    continue
        print(file_path+f" 文件代码:{count[1]},总代码{count[0]}")
        count[1]=0
sum_code(r"C:\Users\Administrator\Desktop\01python\3.0")
print("#"*200)
print(f"读取py文件一共:{file_count[0]}")
print(f"总代码量为:{count[0]}")

猜你喜欢

转载自www.cnblogs.com/zx125/p/11363324.html