|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
下面的代码是第35课第4题,在第一个函数打开文件那里总会报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0x88 in position 118: illegal multibyte sequence。不知道为啥,帮忙看一下,谢谢。
import easygui as g
import os
Sum = 0
list1 = []
count_code = {}
file_dict = {}
def countf(file_name,ext):
count = 0
count_code.setdefault(ext,0)
with open(file_name) as f:
file_list = list(f)
count = len(file_list)
count_code[ext] += count
return count_code
def count_file(ext):
file_dict.setdefault(ext,0)
file_dict[ext] += 1
return file_dict
def file_type(path):
os.chdir(path)
all_files = os.listdir(path)
for each_file in all_files:
if os.path.isdir(each_file):
file_type(each_file)
os.chdir(os.pardir)
elif os.path.isfile(each_file):
ext = os.path.splitext(each_file)[1]
if ext in '.py, .c, .cpp, .asm, .pas, .java':
dict1 = count_file(ext)
dict2 = countf(each_file,ext)
for i in dict2.keys():
Sum += dict2[i]
n = (Sum/100000)*100
left = 100000 - Sum
for i in dict1.keys():
list1.append('[%s]源文件 %d 个,源代码 %d 行' %(i,dict1[i],dict2[i]))
g.textbox('您目前共累计编写了%d行代码,完成进度:%.2f %,离10万行代码还差%d行' % (Sum,n,left),'统计结果',text=list1)
path = g.diropenbox('请选择您的代码库')
file_type(path) |
|