最近在学习Python,写了不少代码,想要统计一下自己到底写了多少行就写了个小程序来统计自己写的代码,目前还不够完善,后期再做修改。
import os
import sys
import os.path
def count_file_lines(file_path):#计算一个文件里面有多少行代码
line_count = 0
flag =True
encoding_type=""
#由于编码分UTF-8,和gbk,为了在统计的时候不报编码的错误,做如下操作
try:
fp = open(file_path,"r",encoding="utf-8")
encoding_type="utf-8"
fp.readline()
#print("执行try")
fp.close()
except UnicodeDecodeError:
encoding_type="gbk"
#print("执行except")
with open(file_path,"r",encoding=encoding_type) as fp:
for line in fp:
if line.strip() == "":
continue
else:
if line.strip().endswith("'''") and flag == False:#不统计注释
flag = True
continue
if line.strip().endswith('"""') and flag == False:#不统计注释
flag = True
continue
if flag == False: