马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
编程的主要目的:利用easygui模块,让用户选择代码文件目录,检查每个文件夹下每个文件的代码行数,并累积求和。
编程的主要思路:先利用diropenbox()让用户选择目录,再以二进制的形式打开每个文件并转化成列表,用len计算行数,用字典的形式保存每个后缀名和行数。
整体函数用了递归。但是程序运行错后,只能用idle把递归部分删去检查,发现运行8次,系统就会保错,不太清楚原因。大家有什么好的方法检查程序?
这个整个运行的结果
def dir_file(catalog):
while True:
dir_name = catalog
catalog_list = os.listdir(dir_name)
for each in catalog_list:
if os.path.isfile(each):
file_name = dir_name + os.sep + each
file = open(file_name,'rb')
file_len = len(list(file))
postfix = each.split('.')
if postfix[1] not in type_dict:
type_dict[postfix[1]] = file_len
file_dict[postfix[1]] = 1
else:
type_dict[postfix[1]] += file_len
file_dict[postfix[1]] += 1
file.close()
else:
dir_name = dir_name + os.sep + each
dir_file(dir_name)
break
import easygui as g
import os
catalog = g.diropenbox('请选择您的代码库','浏览文件夹')
type_dict = {} #存放文件后缀和行数
file_dict = {}
dir_file(catalog)
dict_msg = type_dict.items() #导出存放类型行数字典
s_text = [] #存放统计结果的文本内容
all_len = 0 #总行数
for dict_each in dict_msg:
each_text = '【.%s】源文件%d个,源代码%d行\n'% (dict_each[0],file_dict[dict_each[0]],dict_each[1])
s_text.append(each_txt)
all_len += dict_each[1]
plan = str(('%.2f'% ((all_len/100000)*100)))+ '%'
s_msg = '您目前共累积编写了%d行代码,完成进度:%d\n离10万行代码还差%d行,请继续努力!'%(all_len,plan,(100000-all_len))
g.textbox(s_msg,'统计结果',s_txet)
这个是去掉递归的运行和检查图片
import easygui as g
import os
catalog = g.diropenbox('请选择您的代码库','浏览文件夹')
type_dict = {} #存放文件后缀和行数
file_dict = {}
dir_name = catalog
catalog_list = os.listdir(dir_name)
for each in catalog_list:
if os.path.isfile(each):
file_name = dir_name + os.sep + each
file = open(file_name,'rb')
file_len = len(list(file))
postfix = each.split('.')
if postfix[1] not in type_dict:
type_dict[postfix[1]] = file_len
file_dict[postfix[1]] = 1
else:
type_dict[postfix[1]] += file_len
file_dict[postfix[1]] += 1
else:
dir_name = dir_name + os.sep + each
dict_msg = type_dict.items() #导出存放类型行数字典
s_text = [] #存放统计结果的文本内容
all_len = 0 #总行数
for dict_each in dict_msg:
each_text = '【.%s】源文件%d个,源代码%d行\n'% (dict_each[0],file_dict[dict_each[0]],dict_each[1])
s_text.append(each_txt)
all_len += dict_each[1]
plan = str(('%.2f'% ((all_len/100000)*100)))+ '%'
s_msg = '您目前共累积编写了%d行代码,完成进度:%d\n离10万行代码还差%d行,请继续努力!'%(all_len,plan,(100000-all_len))
g.textbox(s_msg,'统计结果',s_txet)
|