binary-husky/gpt_academic · error · ValueError

不是一个文件: {path}

Error message

不是一个文件: {path}

What it means

Second precondition of MarkdownConverter._validate_file: the path exists but is not a regular file (directory, symlink-to-dir, special file), raising ValueError('不是一个文件: <path>'). Means the earlier existence check passed and the type check is what failed.

Source

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

        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))}"
            )

        return path

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Enumerate the directory's *.pdf files and convert each individually
  2. Fix the upstream path construction so the file (not its parent) is passed
  3. Validate in the UI that the selection is a file

Example fix

// before
converter.convert('papers/')  # 不是一个文件

// after
for pdf in Path('papers').glob('*.pdf'):
    if pdf.is_file():
        converter.convert(pdf)
Defensive patterns

Strategy: type-guard

Validate before calling

from pathlib import Path

p = Path(target)
if p.is_dir():
    raise ValueError('pass a PDF file, not its folder')

Type guard

from pathlib import Path

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

Try / catch

try:
    md = converter.convert(p)
except ValueError as e:
    if '不是一个文件' in str(e) and p.is_dir():
        for pdf in sorted(p.glob('*.pdf')): convert_one(pdf)
    else: raise

Prevention

When it happens

Trigger: Passing a directory containing PDFs; an output path accidentally used as input; a glob pattern that matched a directory.

Common situations: Batch UIs allowing folder selection; users pasting the containing folder instead of the file.

Related errors


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