艺小卤 发表于 2025-4-4 14:05:43

代码问题

import easygui as g
import os

def show_result(start_dir):
    lines = 0
    total = 0
    text = ""

    for i in source_list:
      lines = source_list
      total += lines
      text += "【%s】源文件 %d 个,源代码 %d 行\n" % (i, file_list, 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) :
    os.chdir(start_dir)
   
    for each_file in os.listdir(os.curdir) :
      ext = os.path.splitext(each_file)
      if ext in target :
            lines = calc_code(each_file) # 统计行数
            # 还记得异常的用法吗?如果字典中不存,抛出 KeyError,则添加字典键
            # 统计文件数
            try:
                file_list += 1
            except KeyError:
                file_list = 1
            # 统计源代码行数
            try:
                source_list += lines
            except KeyError:
                source_list = lines
            
      if os.path.isdir(each_file) :
            search_file(each_file) # 递归调用
            os.chdir(os.pardir) # 递归调用后切记返回上一层目录
            
target = ['.c', '.cpp', '.py', '.cc', '.java', '.pas', '.asm']
file_list = {}
source_list = {}

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

search_file(path)
show_result(path)

这个代码怎么运行不起来?

FishC 发表于 2025-4-4 14:06:20

代码问题分析及修正方案

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

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

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

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

修正后的完整代码:


import easygui as g
import os

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

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

def calc_code(filename):
    lines = 0
    try:
      with open(filename, 'r', encoding='utf-8') as f:
            for line in f:
                if line.strip():# 跳过空行
                  lines += 1
    except UnicodeDecodeError:
      return 0
    return lines

def search_file(start_dir):
    global file_count, total_lines
    os.chdir(start_dir)
   
    for each_file in os.listdir():
      if os.path.splitext(each_file) in target:
            lines = calc_code(each_file)
            if lines > 0:
                file_count += 1
                total_lines += lines
               
      if os.path.isdir(each_file):
            search_file(each_file)
            os.chdir(os.pardir)

g.msgbox("请选择代码库目录", "代码统计")
path = g.diropenbox("请选择目录:")
if path:
    search_file(path)
    show_result()


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

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

player-none 发表于 2025-4-4 20:19:16

你哪里搞来的那么多程序{:10_245:}
页: [1]
查看完整版本: 代码问题