Python 该死的作用域的问题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)
for each_line in fi:
if not each_line.isspace():
count += 1
count_list.append(count)
fi.close()
else:
pass
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))
|