|
8鱼币
- import easygui as g
- import os
- def show_result(start_dir):
- lines = 0
- total = 0
- text = ""
- for i in source_list:
- lines = source_list[i]
- total += lines
- text += "【%s】源文件 %d 个,源代码 %d 行\n" % (i, file_list[i], lines)
- title = '统计结果'
- msg = '您目前共累积编写了 %d 行代码,完成进度:%.2f %%\n离 10 万行代码还差 %d 行,请继续努力!' % (total, total / 1000, 100000 - total)
- g.textbox(msg, title, text)
- def calc_code(file_name):
- lines = 0
- with open(file_name) as f:
- print('正在分析文件:%s ...' % file_name)
- try:
- for each_line in f:
- lines += 1
- except UnicodeDecodeError:
- pass # 不可避免会遇到格式不兼容的文件,这里忽略掉......
- return lines
- def search_file(start_dir):
- if 'D:\\python file' in start_dir or 'D:\\python project' in start_dir or start_dir == 'D:\\':
- #print (1)
- os.chdir(start_dir)
- for each_file in os.listdir(os.curdir):
- ext = os.path.splitext(each_file)[1]
- if ext in target:
- lines = calc_code(each_file) # 统计行数
- # 还记得异常的用法吗?如果字典中不存,抛出 KeyError,则添加字典键
- # 统计文件数
- try:
- file_list[ext] += 1
- except KeyError:
- file_list[ext] = 1
- # 统计源代码行数
- try:
- source_list[ext] += lines
- except KeyError:
- source_list[ext] = lines
- if os.path.isdir(each_file):
- search_file(each_file) # 递归调用
- os.chdir(os.pardir) # 递归调用后切记返回上一层目录
- else:
- pass
- target = ['.c', '.cpp', '.py', '.cc', '.java', '.pas', '.asm']
- file_list = {}
- source_list = {}
- g.msgbox("请打开您存放所有代码的文件夹......", "统计代码量")
- path = g.diropenbox("请选择您的代码库:")
- search_file(path)
- show_result(path)
复制代码
本帖最后由 阿奇_o 于 2021-7-6 18:09 编辑
你的思路,搞复杂了,简单点:
先实现 针对一个指定目录内的所有.py文件,进行统计,然后再“泛化”。
ps: 最好不要 修改当前工作路径,(即尽量不用 os.chdir(),否则可能会搞乱或意外),
递归查看所有文件夹里的文件(不用自己实现),只需要用 os.walk() 即可。
另外,路径,尽量用 os.path.join() 。
---- 例如
- import os
- def count_lines(path=r'.\\数据结构', ext='.py'):
- cnt = {}
- for dirpath, dirnames, files in os.walk(path):
- # print(dirpath, files)
- for f in files:
- f_path = os.path.join(dirpath, f)
- if os.path.splitext(f_path)[1] == ext:
- with open(f_path, mode='r', encoding='utf-8') as cf:
- cnt[f_path] = len(cf.readlines())
- return cnt
- pylines = count_lines()
- for i in pylines.items():
- print(*i)
- print(f".py代码的总行数:{sum(pylines.values())}")
复制代码
|
最佳答案
查看完整内容
你的思路,搞复杂了,简单点:
先实现 针对一个指定目录内的所有.py文件,进行统计,然后再“泛化”。
ps: 最好不要 修改当前工作路径,(即尽量不用 os.chdir(),否则可能会搞乱或意外),
递归查看所有文件夹里的文件(不用自己实现),只需要用 os.walk() 即可。
另外,路径,尽量用 os.path.join() 。
---- 例如
|