{"record":{"id":"13501ce32de1d657","repo":"fxsjy/jieba","slug":"jieba-file-does-not-exist-s","errorCode":null,"errorMessage":"jieba: file does not exist: %s","messagePattern":"jieba: file does not exist: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"jieba/__init__.py","lineNumber":513,"sourceCode":"                width = len(w)\n                if len(w) > 2:\n                    for i in xrange(len(w) - 1):\n                        gram2 = w[i:i + 2]\n                        if self.FREQ.get(gram2):\n                            yield (gram2, start + i, start + i + 2)\n                if len(w) > 3:\n                    for i in xrange(len(w) - 2):\n                        gram3 = w[i:i + 3]\n                        if self.FREQ.get(gram3):\n                            yield (gram3, start + i, start + i + 3)\n                yield (w, start, start + width)\n                start += width\n\n    def set_dictionary(self, dictionary_path):\n        with self.lock:\n            abs_path = _get_abs_path(dictionary_path)\n            if not os.path.isfile(abs_path):\n                raise Exception(\"jieba: file does not exist: \" + abs_path)\n            self.dictionary = abs_path\n            self.initialized = False\n\n\n# default Tokenizer instance\n\ndt = Tokenizer()\n\n# global functions\n\nget_FREQ = lambda k, d=None: dt.FREQ.get(k, d)\nadd_word = dt.add_word\ncalc = dt.calc\ncut = dt.cut\nlcut = dt.lcut\ncut_for_search = dt.cut_for_search\nlcut_for_search = dt.lcut_for_search\ndel_word = dt.del_word","sourceCodeStart":495,"sourceCodeEnd":531,"githubUrl":"https://github.com/fxsjy/jieba/blob/67fa2e36e72f69d9134b8a1037b83fbb070b9775/jieba/__init__.py#L495-L531","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\njieba.set_dictionary('dict.txt.big')  # depends on cwd\n\n# after\nimport os\nDICT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dict.txt.big')\njieba.set_dictionary(DICT)","handlingStrategy":"validation","validationCode":"import os\ndef set_dict_safe(path):\n    p = os.path.abspath(path)\n    if not os.path.isfile(p):\n        raise FileNotFoundError(p)\n    jieba.set_dictionary(p)","typeGuard":null,"tryCatchPattern":"try:\n    jieba.set_dictionary(p)\nexcept Exception as e:\n    if 'does not exist' in str(e):\n        jieba.set_dictionary(default_bundled_dict)","preventionTips":["Build paths from __file__, never bare relative paths","Bundle dictionaries as package data","Fail fast on missing resources at startup"],"tags":["jieba","file-not-found","dictionary","path"],"backgroundTag":"file-not-found","analyzedSha":"67fa2e36e72f69d9134b8a1037b83fbb070b9775","analyzedAt":"2026-08-27T11:28:25.101Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}