binary-husky/gpt_academic · error · FileNotFoundError

文件{file}不存在

Error message

文件{file}不存在

What it means

Raised by promote_file_to_downloadzone in toolbox.py:428 when the file path passed in does not exist on disk. The function copies a generated file into the per-user downloadzone folder so the Gradio UI can offer it as a download; the os.path.exists check at the top is a fail-fast guard before any copying happens. Seeing it means a plugin produced (or claimed to produce) a file path that was never actually written or was already deleted.

Source

Thrown at toolbox.py:428

        child_path = os.path.abspath(file)
        if os.path.samefile(os.path.commonpath([parent_path, child_path]), parent_path):
            return True
        else:
            return False
    except:
        return False


def promote_file_to_downloadzone(file:str, rename_file:str=None, chatbot:ChatBotWithCookies=None):
    # 将文件复制一份到下载区
    import shutil

    if chatbot is not None:
        user_name = get_user(chatbot)
    else:
        user_name = default_user_name
    if not os.path.exists(file):
        raise FileNotFoundError(f"文件{file}不存在")
    user_path = get_log_folder(user_name, plugin_name=None)
    if file_already_in_downloadzone(file, user_path):
        new_path = file
    else:
        user_path = get_log_folder(user_name, plugin_name="downloadzone")
        if rename_file is None:
            rename_file = f"{gen_time_str()}-{os.path.basename(file)}"
        new_path = pj(user_path, rename_file)
        # 如果已经存在,先删除
        if os.path.exists(new_path) and not os.path.samefile(new_path, file):
            os.remove(new_path)
        # 把文件复制过去
        if not os.path.exists(new_path):
            shutil.copyfile(file, new_path)
    # 将文件添加到chatbot cookie中
    if chatbot is not None:
        if "files_to_promote" in chatbot._cookies:
            current = chatbot._cookies["files_to_promote"]

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Check with os.path.exists / ls whether the upstream step actually wrote the file, and fix the generation step that failed.
  2. If the file was written, verify the path passed to promote_file_to_downloadzone is absolute or resolved relative to the same base directory used at write time (use pj(get_log_folder(...), name) consistently).
  3. Guard the call: only promote when the file exists, otherwise surface a user-facing message in chatbot instead of crashing.
  4. If files are vanishing between steps, inspect concurrent cleanup logic (log folder pruning) and promote before cleanup or write to a stable location.

Example fix

# before
promote_file_to_downloadzone(res_path, chatbot=chatbot)  # crashes if render failed

# after
if os.path.exists(res_path):
    promote_file_to_downloadzone(res_path, chatbot=chatbot)
else:
    chatbot.append((None, f"[local] 生成失败,未找到文件: {res_path}"))
Defensive patterns

Strategy: type-guard

Validate before calling

import os

def safe_promote(file: str, chatbot=None, rename_file: str = None):
    if not file or not os.path.isfile(os.path.abspath(file)):
        return None  # caller decides how to report
    promote_file_to_downloadzone(file, rename_file=rename_file, chatbot=chatbot)
    return file

Type guard

def is_promotable_file(file) -> bool:
    return isinstance(file, str) and len(file) > 0 and os.path.isfile(file)

Try / catch

try:
    promote_file_to_downloadzone(res_path, chatbot=chatbot)
except FileNotFoundError as e:
    # upstream generation failed; tell the user instead of crashing the plugin
    chatbot.append((None, f'[local] output file missing, generation step likely failed: {e}'))
    yield from update_chatbot(chatbot)

Prevention

When it happens

Trigger: Calling promote_file_to_downloadzone(file, ...) where file points to a path that was never created (upstream step failed silently), a file deleted by a cleanup step between generation and promotion, or a relative path resolved against an unexpected working directory. Common with plugins that build an output path with pj(...) but skip writing when the LLM/tool output is empty.

Common situations: A plugin's render/export step failed (e.g. LLM returned nothing) so the output .md/.pdf/.tex file was never generated, but the promotion call still runs. Log-folder cleanup or a restarted backend removed temp files. Path built from user-supplied filename containing illegal characters so the earlier write failed.

Related errors


AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14). Data as JSON: /api/errors/a3e5192ab7555adc. Report an issue: GitHub.