|
|
40鱼币
求助,python第三十五讲作业里的最后一题,大神帮我看看是哪里出了问题啊,老报错
import easygui as g
import os
read= []
def collect(every_parts):
if every_parts != [[],0]:
for each in every_parts[0]:
print(each)
file_open = open(each)
read = file_open.read()
for each in read:
if each == '\n':
every_parts[1] += 1
file_open.close()
return every_parts[1]
else:
return 0
file_path = g.diropenbox('请选择您的代码库:','浏览文件夹','E:\\')
total=0
collect_py=[[],0]
collect_c=[[],0]
collect_cpp=[[],0]
collect_pas=[[],0]
collect_asm=[[],0]
for path,folder,file in os.walk(file_path):
for each_file in file:
(name,extention)=os.path.splitext(path+'\\'+each_file)
if extention == '.py':
collect_py[0].append(path+'\\'+each_file)
elif extention == '.c':
collect_c.append(path+'\\'+each_file)
elif extention == '.cpp':
collect_cpp.append(path+'\\'+each_file)
elif extention == '.pas':
collect_pas.append(path+'\\'+each_file)
elif extention == '.asm':
collect_asm.append(path+'\\'+each_file)
else:
continue
num_py = collect(collect_py)
num_c = collect(collect_c)
num_cpp = collect(collect_cpp)
num_pas = collect(collect_pas)
num_asm = collect(collect_asm)
total = num_py + num_c + num_cpp + num_pas + num_asm
finish = total / 1000
other = 100000 - total
msg = '您目前共累计编写了' + str(total) + '行代码,完成进度' + str(finish) + '%\n' + '离十万行代码还差' + str(other) + '行,请继续努力!'
title = '统计结果'
text = '【.py】源文件'+str(len(collect_py[0]))+'个,源代码'+str(num_py)+'行\n'+'【.c】源文件'+str(len(collect_c[0]))+'个,源代码'+str(num_c)+'行\n'+'【.cpp】源文件'+str(len(collect_cpp[0]))+'个,源代码'+str(num_cpp)+'行\n'+'【.pas】源文件'+str(len(collect_pas[0]))+'个,源代码'+str(num_pas)+'行\n'+'【.asm】源文件'+str(len(collect_asm[0]))+'个,源代码'+str(num_asm)+'行\n'
g.textbox(msg,title,text)
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\homework0353.py", line 44, in <module>
num_py = collect(collect_py)
File "C:\Users\Administrator\Desktop\homework0353.py", line 10, in collect
read = file_open.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 159: illegal multibyte sequence |
最佳答案
查看完整内容
这是一个非常麻烦的问题,原因是源文件有中文,而中文的编码类型又不同,比如GBK,UTF-8等等,你可以将第9行改为 file_open = open(each,encoding='utf-8')
有可能解决。
我试了一下,最后输出如图。
|