|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 小菜鸟FLY 于 2021-7-28 07:15 编辑
这是仿照小甲鱼的程序写的一个可以查找指定文件夹里写的代码量,实现的时候如果文件夹里面还有文件夹,就会出错:
Traceback (most recent call last):
File "D:\mypython\python_py\统计代码量界面.py", line 27, in <module>
cc=search_file(path)#此函数返回字典(名字:行数)
File "D:\mypython\python_py\统计代码量界面.py", line 16, in search_file
with open(each_file,'rb') as f:
FileNotFoundError: [Errno 2] No such file or directory:'wo.py'
我用的是迭代去查找文件夹,不知道怎么去修改
- import easygui as g
- import os
- #递归搜索各个文件
- def search_file(start_file):
- os.chdir(start_file)#将指定路径设置成当前工作空间
- file_name=os.listdir(os.curdir)
- line_numbers=0
- result_dict={}
- for each_file in file_name:
- if os.path.isdir(each_file):
- search_file(each_file)
- else:
- file_type=os.path.splitext(each_file)[1]
- if file_type in target:
- with open(each_file,'rb') as f:
- for i in f:
- line_numbers+=1
- result_dict[each_file]=line_numbers
- line_numbers=0
- return result_dict
- #主程序
- target=['.py','.c','.cpp','.java','.asm','.cc']#文件类型
- path=g.diropenbox('请选择您的代码库:')
- cc=search_file(path)#此函数返回字典(名字:行数)
- aa=list(cc.values())
- print(cc.items())
- file_number=len(aa)
- result=sum(aa)
- o=float(result/1000)
- ok=str(o)+'%'
- msg='目标:十万行代码'
- title='统计代码量'
- fields=['代码篇数:','总共代码行数:','完成目标进度:']
- values=[file_number,result,ok]
- g.multenterbox(msg, title, fields,values)
复制代码
|
|