binary-husky/gpt_academic · error · ValueError

文件不存在: {path}

Error message

文件不存在: {path}

What it means

First precondition of MarkdownConverter._validate_file (the markitdown-based PDF→Markdown converter): the resolved path does not exist, so ValueError('文件不存在: <abs path>') is raised. Identical pattern to the Excel reader's check but for this class, whose SUPPORTED_EXTENSIONS is only {'.pdf'}.

Source

Thrown at crazy_functions/doc_fns/read_fns/markitdown/markdown_reader.py:121

    def _validate_file(self, file_path: Union[str, Path], max_size_mb: int = 100) -> Path:
        """验证文件

        Args:
            file_path: 文件路径
            max_size_mb: 允许的最大文件大小(MB)

        Returns:
            Path: 验证后的Path对象

        Raises:
            ValueError: 文件不存在、格式不支持或大小超限
            PermissionError: 没有读取权限
        """
        path = Path(file_path).resolve()

        if not path.exists():
            raise ValueError(f"文件不存在: {path}")

        if not path.is_file():
            raise ValueError(f"不是一个文件: {path}")

        if not os.access(path, os.R_OK):
            raise PermissionError(f"没有读取权限: {path}")

        file_size_mb = path.stat().st_size / (1024 * 1024)
        if file_size_mb > max_size_mb:
            raise ValueError(
                f"文件大小 ({file_size_mb:.1f}MB) 超过限制 {max_size_mb}MB"
            )

        if path.suffix.lower() not in self.SUPPORTED_EXTENSIONS:
            raise ValueError(
                f"不支持的格式: {path.suffix}. "
                f"支持的格式: {', '.join(sorted(self.SUPPORTED_EXTENSIONS))}"
            )

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Verify the absolute path shown in the message; construct paths with Path and .resolve()
  2. If chaining conversions (docx→pdf→md), reuse the returned path string from the previous step rather than rebuilding it
  3. Guard against TOCTOU by checking existence immediately before the call in the same process

Example fix

// before
md = converter.convert('out/paper')  # forgot .pdf suffix -> 文件不存在

// after
pdf = word2pdf.convert_to_pdf(docx)   # returns the real path
md = converter.convert(pdf)
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path

pdf = Path(pdf_path).resolve()
if not pdf.exists():
    raise FileNotFoundError(f'{pdf} missing; was the PDF step successful?')

Type guard

def is_pdf_file(v) -> bool:
    p = Path(v)
    return p.is_file() and p.suffix.lower() == '.pdf'

Try / catch

try:
    md = converter.convert(pdf)
except ValueError as e:
    if '文件不存在' in str(e):
        regenerate_or_relocate_pdf()
    raise

Prevention

When it happens

Trigger: Passing a non-existent PDF path, a relative path resolved from the wrong CWD, or a URL where a local file path is expected.

Common situations: Generated temp PDFs (e.g. from a Word→PDF step) referenced under a different name after rename/cleanup; concurrent cleanup deleting the file before conversion.

Related errors


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