fxsjy/jieba · error · Exception
jieba: file does not exist: %s
Error message
jieba: file does not exist: %s
What it means
set_dictionary resolves the given path (relative to cwd or absolute) and verifies it exists as a file before switching jieba's main dictionary. If os.path.isfile fails it raises this Exception with the resolved absolute path. This forces re-initialization, so an invalid path would otherwise break all later cut() calls.
Source
Thrown at jieba/__init__.py:513
width = len(w)
if len(w) > 2:
for i in xrange(len(w) - 1):
gram2 = w[i:i + 2]
if self.FREQ.get(gram2):
yield (gram2, start + i, start + i + 2)
if len(w) > 3:
for i in xrange(len(w) - 2):
gram3 = w[i:i + 3]
if self.FREQ.get(gram3):
yield (gram3, start + i, start + i + 3)
yield (w, start, start + width)
start += width
def set_dictionary(self, dictionary_path):
with self.lock:
abs_path = _get_abs_path(dictionary_path)
if not os.path.isfile(abs_path):
raise Exception("jieba: file does not exist: " + abs_path)
self.dictionary = abs_path
self.initialized = False
# default Tokenizer instance
dt = Tokenizer()
# global functions
get_FREQ = lambda k, d=None: dt.FREQ.get(k, d)
add_word = dt.add_word
calc = dt.calc
cut = dt.cut
lcut = dt.lcut
cut_for_search = dt.cut_for_search
lcut_for_search = dt.lcut_for_search
del_word = dt.del_wordView on GitHub (pinned to 67fa2e36e7)
Solutions
- Use an absolute path built from __file__: os.path.join(os.path.dirname(__file__), 'mydict.txt')
- Verify with os.path.isfile(path) before calling set_dictionary
- If packaging, include the dictionary as package data and reference it via importlib.resources or pkg_resources
Example fix
# before
jieba.set_dictionary('dict.txt.big') # depends on cwd
# after
import os
DICT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dict.txt.big')
jieba.set_dictionary(DICT) Defensive patterns
Strategy: validation
Validate before calling
import os
def set_dict_safe(path):
p = os.path.abspath(path)
if not os.path.isfile(p):
raise FileNotFoundError(p)
jieba.set_dictionary(p) Try / catch
try:
jieba.set_dictionary(p)
except Exception as e:
if 'does not exist' in str(e):
jieba.set_dictionary(default_bundled_dict) Prevention
- Build paths from __file__, never bare relative paths
- Bundle dictionaries as package data
- Fail fast on missing resources at startup
When it happens
Trigger: Calling jieba.set_dictionary('dict.txt') when the file doesn't exist, when the relative path is resolved against the current working directory (not the script's directory), or when a directory is passed.
Common situations: Running a script from a different cwd so relative paths break; packaging code where the dict isn't bundled into site-packages/wheel; typos in the path; passing a path that exists only on the dev machine.
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
- invalid dictionary entry in %s at Line %s: %s
- jieba: file does not exist: %s
- invalid POS dictionary entry in %s at Line %s: %s
- dictionary file %s must be utf-8
- jieba: the input parameter should be unicode.
AI-assisted analysis of fxsjy/jieba@67fa2e36e7 (2026-08-27).
Data as JSON: /api/errors/13501ce32de1d657.
Report an issue: GitHub.