鱼C论坛

 找回密码
 立即注册
查看: 133|回复: 2

代码问题

[复制链接]
发表于 2025-4-4 14:05:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import easygui as g
  2. import os

  3. def show_result(start_dir):
  4.     lines = 0
  5.     total = 0
  6.     text = ""

  7.     for i in source_list:
  8.         lines = source_list[i]
  9.         total += lines
  10.         text += "【%s】源文件 %d 个,源代码 %d 行\n" % (i, file_list[i], lines)
  11.     title = '统计结果'
  12.     msg = '您目前共累积编写了 %d 行代码,完成进度:%.2f %%\n离 10 万行代码还差 %d 行,请继续努力!' % (total, total/1000, 100000-total)
  13.     g.textbox(msg, title, text)

  14. def calc_code(file_name):
  15.     lines = 0
  16.     with open(file_name) as f:
  17.         print('正在分析文件:%s ...' % file_name)
  18.         try:
  19.             for each_line in f:
  20.                 lines += 1
  21.         except UnicodeDecodeError:
  22.             pass # 不可避免会遇到格式不兼容的文件,这里忽略掉......
  23.     return lines

  24. def search_file(start_dir) :
  25.     os.chdir(start_dir)
  26.    
  27.     for each_file in os.listdir(os.curdir) :
  28.         ext = os.path.splitext(each_file)[1]
  29.         if ext in target :
  30.             lines = calc_code(each_file) # 统计行数
  31.             # 还记得异常的用法吗?如果字典中不存,抛出 KeyError,则添加字典键
  32.             # 统计文件数
  33.             try:
  34.                 file_list[ext] += 1
  35.             except KeyError:
  36.                 file_list[ext] = 1
  37.             # 统计源代码行数
  38.             try:
  39.                 source_list[ext] += lines
  40.             except KeyError:
  41.                 source_list[ext] = lines
  42.             
  43.         if os.path.isdir(each_file) :
  44.             search_file(each_file) # 递归调用
  45.             os.chdir(os.pardir) # 递归调用后切记返回上一层目录
  46.             
  47. target = ['.c', '.cpp', '.py', '.cc', '.java', '.pas', '.asm']
  48. file_list = {}
  49. source_list = {}

  50. g.msgbox("请打开您存放所有代码的文件夹......", "统计代码量")
  51. path = g.diropenbox("请选择您的代码库:")

  52. search_file(path)
  53. show_result(path)
复制代码

这个代码怎么运行不起来?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-4-4 14:06:20 | 显示全部楼层
代码问题分析及修正方案

这个代码存在以下几个主要问题:

1. target变量未初始化
原始代码:target =
修正后:target = ('.py', '.java', '.cpp') (示例扩展名,可根据需要修改)

2. 变量作用域问题
file_list和source_list应该使用global声明或在函数内初始化

3. 字典类型误用
file_list和source_list初始化为字典但被当作整数使用

修正后的完整代码:


  1. import easygui as g
  2. import os

  3. target = ('.py', '.txt')  # 需要统计的文件扩展名
  4. file_count = 0
  5. total_lines = 0

  6. def show_result():
  7.     global total_lines, file_count
  8.     msg = f'共找到{file_count}个源代码文件,累计代码行数:{total_lines}行\n'
  9.     msg += f'进度:{total_lines/1000:.2f}%,剩余目标:{100000 - total_lines}行'
  10.     g.textbox(msg, "统计结果")

  11. def calc_code(filename):
  12.     lines = 0
  13.     try:
  14.         with open(filename, 'r', encoding='utf-8') as f:
  15.             for line in f:
  16.                 if line.strip():  # 跳过空行
  17.                     lines += 1
  18.     except UnicodeDecodeError:
  19.         return 0
  20.     return lines

  21. def search_file(start_dir):
  22.     global file_count, total_lines
  23.     os.chdir(start_dir)
  24.    
  25.     for each_file in os.listdir():
  26.         if os.path.splitext(each_file)[1] in target:
  27.             lines = calc_code(each_file)
  28.             if lines > 0:
  29.                 file_count += 1
  30.                 total_lines += lines
  31.                
  32.         if os.path.isdir(each_file):
  33.             search_file(each_file)
  34.             os.chdir(os.pardir)

  35. g.msgbox("请选择代码库目录", "代码统计")
  36. path = g.diropenbox("请选择目录:")
  37. if path:
  38.     search_file(path)
  39.     show_result()
复制代码


主要改进点:
1. 使用全局变量替代原来的字典结构
2. 添加文件编码处理(utf-8)
3. 优化空行过滤逻辑
4. 简化递归目录遍历逻辑
5. 添加路径有效性验证

这个版本应该可以正常运行,建议从简单的目录开始测试(目录中不要包含二进制文件)。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-4-4 20:19:16 | 显示全部楼层
你哪里搞来的那么多程序
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-19 04:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表