total_num =0for base_path,folder_list,file_list inos.walk(target_path):for file_name infile_list:
file_path=os.path.join(base_path,file_name)
file_ext= file_path.rsplit('.',maxsplit=1)if len(file_ext) != 2:#没有后缀名
continue
if file_ext[1] != 'py':#不是py文件
continuefile_num=0
with open(file_path,'rb') as f:for line inf:#去空格
line =line.strip()if notline:continue
#去除 # 注释
if line.startswith(b'#'):continuefile_num+= 1total_num+= file_num