fxsjy/jieba · error · Exception

jieba: file does not exist: %s

Error message

jieba: file does not exist: %s

What it means

tfidf.KeywordTokenizer.set_stop_words resolves the stop-word file path and requires it to be an existing file before loading words into the stop-word set. A missing file raises Exception with the resolved absolute path. Note the file must also be UTF-8 (decoded right after this check).

Source

Thrown at jieba/analyse/tfidf.py:26

_get_module_path = lambda path: os.path.normpath(os.path.join(os.getcwd(),
                                                 os.path.dirname(__file__), path))
_get_abs_path = jieba._get_abs_path

DEFAULT_IDF = _get_module_path("idf.txt")


class KeywordExtractor(object):

    STOP_WORDS = set((
        "the", "of", "is", "and", "to", "in", "that", "we", "for", "an", "are",
        "by", "be", "as", "on", "with", "can", "if", "from", "which", "you", "it",
        "this", "then", "at", "have", "all", "not", "one", "has", "or", "that"
    ))

    def set_stop_words(self, stop_words_path):
        abs_path = _get_abs_path(stop_words_path)
        if not os.path.isfile(abs_path):
            raise Exception("jieba: file does not exist: " + abs_path)
        content = open(abs_path, 'rb').read().decode('utf-8')
        for line in content.splitlines():
            self.stop_words.add(line)

    def extract_tags(self, *args, **kwargs):
        raise NotImplementedError


class IDFLoader(object):

    def __init__(self, idf_path=None):
        self.path = ""
        self.idf_freq = {}
        self.median_idf = 0.0
        if idf_path:
            self.set_new_path(idf_path)

    def set_new_path(self, new_idf_path):

View on GitHub (pinned to 67fa2e36e7)

Solutions

  1. Use an absolute path anchored to your module: os.path.join(os.path.dirname(__file__), 'stopwords.txt')
  2. Check os.path.isfile(path) before calling set_stop_words
  3. Bundle the stop-word file as package data when distributing

Example fix

# before
jieba.analyse.set_stop_words('stop_words.txt')

# after
import os
jieba.analyse.set_stop_words(os.path.join(os.path.dirname(__file__), 'stop_words.txt'))
Defensive patterns

Strategy: validation

Validate before calling

import os
p = os.path.join(BASE_DIR, 'stopwords.txt')
assert os.path.isfile(p) and open(p, 'rb').read().decode('utf-8')
jieba.analyse.set_stop_words(p)

Try / catch

try:
    jieba.analyse.set_stop_words(p)
except Exception as e:
    if 'does not exist' in str(e):
        log.warning('stop words missing; using defaults')

Prevention

When it happens

Trigger: Calling jieba.analyse.set_stop_words(path) where path doesn't exist, is a directory, or a relative path resolved from the wrong working directory.

Common situations: Shipping keyword-extraction code without bundling the stop-words file; relative paths breaking when the process cwd changes; typo'd filenames.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of fxsjy/jieba@67fa2e36e7 (2026-08-27). Data as JSON: /api/errors/52fe1bc5ccbb2ae8. Report an issue: GitHub.