|
发表于 2020-5-25 22:17:07
|
显示全部楼层
失文 发表于 2020-5-25 22:15
for each_line in fi:
又出问题了
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in positi ...
改成- import os
- import easygui as g
- def search_file(path):
- os.chdir(path)
- file_list = os.listdir(path)
- for each_file in file_list:
- if os.path.isfile(each_file):
- extension = os.path.splitext(each_file)[1]
- if extension == '.txt':
- global count
- fi = open(each_file,encoding='utf-8')
- for each_line in fi:
- if not each_line.isspace():
- count += 1
- count_list.append(count)
- fi.close()\
- else:
- search_file(each_file)
- os.chdir(os.pardir)
- return count_list
- def calculate(count_list):
- sum1 = 0
- for i in count_list:
- sum1 += i
- return sum1
- path = g.diropenbox('请选择你的代码库', '文件浏览')
- count = 0
- count_list = []
- if path != None:
- b = search_file(path)
- sum_code = calculate(b)
- print('你已经写了%d行代码,还差%d行,继续加油!' % (sum_code, 100000 - sum_code))
复制代码 |
|