hankcs/HanLP · error · FileNotFoundError

{path}

Error message

{path}

What it means

make_debug_corpus accepts either a single corpus file or a directory of files; anything else (nonexistent path, or a special file) raises FileNotFoundError with just the path.

Source

Thrown at hanlp/utils/io_util.py:58

    if verbose:
        src = TimingFileIterator(path)
    else:
        src = open(path, encoding='utf-8')
    for line in src:
        yield json.loads(line)
    if not verbose:
        src.close()


def make_debug_corpus(path, delimiter=None, percentage=0.1, max_samples=100):
    files = []
    if os.path.isfile(path):
        files.append(path)
    elif os.path.isdir(path):
        files += [os.path.join(path, f) for f in os.listdir(path) if
                  os.path.isfile(os.path.join(path, f)) and '.debug' not in f and not f.startswith('.')]
    else:
        raise FileNotFoundError(path)
    for filepath in files:
        filename, file_extension = os.path.splitext(filepath)
        if not delimiter:
            if file_extension in {'.tsv', '.conll', '.conllx', '.conllu'}:
                delimiter = '\n\n'
            else:
                delimiter = '\n'
        with open(filepath, encoding='utf-8') as src, open(filename + '.debug' + file_extension, 'w',
                                                           encoding='utf-8') as out:
            samples = src.read().strip().split(delimiter)
            max_samples = min(max_samples, int(len(samples) * percentage))
            out.write(delimiter.join(samples[:max_samples]))


def path_join(path, *paths):
    return os.path.join(path, *paths)

View on GitHub (pinned to ddb1299bdd)

Solutions

  1. Verify the path exists and is a regular file or directory: os.path.exists(path) before the call.
  2. If it is a symlink/special file, resolve with os.path.realpath or point directly at the target file.
  3. Check cwd-relative vs absolute path mistakes in scripts and notebooks.

Example fix

# before
make_debug_corpus('data/train.tsv')  # cwd changed
# after
make_debug_corpus(os.path.abspath('data/train.tsv'))
Defensive patterns

Strategy: validation

Validate before calling

import os
assert os.path.exists(path), f'corpus path not found: {path}'

Prevention

When it happens

Trigger: Calling make_debug_corpus(path) where path is neither os.path.isfile nor os.path.isdir — usually a typo, a missing mount, or a path with wrong casing/extension.

Common situations: Notebooks referencing relative paths from a changed cwd; data not yet downloaded; container volumes not mounted where expected.

Related errors


AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27). Data as JSON: /api/errors/ff320a75b9b14862. Report an issue: GitHub.