{"record":{"id":"c8378c6ac58c42fe","repo":"soxoj/maigret","slug":"problem-while-attempting-to-access-data-file-fil","errorCode":null,"errorMessage":"Problem while attempting to access data file '{filename}'.","messagePattern":"Problem while attempting to access data file '(.+?)'\\.","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"maigret/sites.py","lineNumber":595,"sourceCode":"        else:\n            raise FileNotFoundError(\n                f\"Bad response while accessing \" f\"data file URL '{url}'.\"\n            )\n\n        return self.load_from_json(data)\n\n    def load_from_file(self, filename: \"str\") -> \"MaigretDatabase\":\n        try:\n            with open(filename, \"r\", encoding=\"utf-8\") as file:\n                try:\n                    data = json.load(file)\n                except Exception as error:\n                    raise ValueError(\n                        f\"Problem parsing json contents from \"\n                        f\"file '{filename}':  {str(error)}.\"\n                    )\n        except FileNotFoundError as error:\n            raise FileNotFoundError(\n                f\"Problem while attempting to access \" f\"data file '{filename}'.\"\n            ) from error\n\n        return self.load_from_json(data)\n\n    def get_scan_stats(self, sites_dict):\n        sites = sites_dict or self.sites_dict\n        found_flags: Dict[str, int] = {}\n        for _, s in sites.items():\n            if \"presense_flag\" in s.stats:\n                flag = s.stats[\"presense_flag\"]\n                found_flags[flag] = found_flags.get(flag, 0) + 1\n\n        return found_flags\n\n    def extract_ids_from_url(self, url: str) -> dict:\n        results = {}\n        for s in self._sites:","sourceCodeStart":577,"sourceCodeEnd":613,"githubUrl":"https://github.com/soxoj/maigret/blob/41674631a92a4c3ea630d046ba125cb750c03798/maigret/sites.py#L577-L613","documentation":"load_from_file() raises FileNotFoundError('Problem while attempting to access data file ...') when open(filename) itself fails with FileNotFoundError — i.e. the path does not exist, a directory component is missing, or (on some platforms) the path is a directory. The original OSError is chained via 'from error', preserving the underlying cause. Because relative paths resolve against the process CWD, the most common cause is running the script from a different directory than the data file.","triggerScenarios":"Calling db.load_from_file('resources/data.json') when the file or the resources/ directory does not exist relative to the current working directory; passing an absolute path with a typo; deploying code where the data file was not packaged into the container/wheel.","commonSituations":"Running the tool from a different CWD than the checkout (relative path breaks), Docker images or PyInstaller bundles that omit resources/, cloned repo without fetched data files, or CI jobs on a fresh checkout where resources/data.json lives elsewhere.","solutions":["Print/verify the resolved path first: os.path.abspath(filename) and os.path.isfile(filename); fix the path or CWD.","Prefer absolute paths derived from your package location: pathlib.Path(__file__).parent / 'data.json'.","If the file is missing from a deployment, copy/vendor maigret's resources/data.json into your distribution or download it at startup.","For missing packaged assets in Docker/bundles, add the file to the image/build and reference it by absolute path."],"exampleFix":"# before\ndb = MaigretDatabase().load_from_file('resources/data.json')  # breaks when CWD differs\n\n# after\nfrom pathlib import Path\nDB_PATH = Path(__file__).resolve().parent / 'resources' / 'data.json'\nassert DB_PATH.is_file(), f'missing maigret db at {DB_PATH}'\ndb = MaigretDatabase().load_from_file(str(DB_PATH))","handlingStrategy":"validation","validationCode":"import os\n\npath = os.path.abspath('resources/data.json')\nif not os.path.isfile(path):\n    raise SystemExit(f'maigret data file not found at {path}')\ndb = MaigretDatabase().load_from_file(path)","typeGuard":"import os\n\ndef file_exists(path: str) -> bool:\n    return os.path.isfile(os.path.abspath(path))","tryCatchPattern":"try:\n    db = MaigretDatabase().load_from_file(path)\nexcept FileNotFoundError as err:\n    logger.error('data file missing: %s', err)\n    path = download_or_locate_db()  # fetch known-good copy\n    db = MaigretDatabase().load_from_file(path)","preventionTips":["Reference data files with paths anchored to __file__ or absolute paths, never CWD-relative ones.","Include resources/data.json in Docker images and bundles; verify with a smoke test at startup.","Use MaigretDatabase.default_db() which resolves the packaged location for you."],"tags":["file-io","file-not-found","path","maigret"],"backgroundTag":"file-not-found","analyzedSha":"41674631a92a4c3ea630d046ba125cb750c03798","analyzedAt":"2026-08-27T02:53:58.946Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}