{"record":{"id":"46d5a963204be430","repo":"python/cpython","slug":"cannot-open-s-for-writing","errorCode":null,"errorMessage":"cannot open '%s' for writing\n","messagePattern":"cannot open '(.+?)' for writing\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Programs/_freeze_module.c","lineNumber":195,"sourceCode":"        size_t i, end = Py_MIN(n + 16, data_size);\n        fprintf(outfile, \"    \");\n        for (i = n; i < end; i++) {\n            fprintf(outfile, \"%u,\", (unsigned int) data[i]);\n        }\n        fprintf(outfile, \"\\n\");\n    }\n    fprintf(outfile, \"};\\n\");\n}\n\nstatic int\nwrite_frozen(const char *outpath, const char *inpath, const char *name,\n             PyObject *marshalled)\n{\n    /* Open the file in text mode. The hg checkout should be using the eol extension,\n       which in turn should cause the EOL style match the C library's text mode */\n    FILE *outfile = fopen(outpath, \"w\");\n    if (outfile == NULL) {\n        fprintf(stderr, \"cannot open '%s' for writing\\n\", outpath);\n        return -1;\n    }\n\n    fprintf(outfile, \"%s\\n\", header);\n    char *arrayname = get_varname(name, \"_Py_M__\");\n    if (arrayname == NULL) {\n        fprintf(stderr, \"memory error: could not allocate varname\\n\");\n        fclose(outfile);\n        return -1;\n    }\n    write_code(outfile, marshalled, arrayname);\n    free(arrayname);\n\n    if (ferror(outfile)) {\n        fprintf(stderr, \"error when writing to '%s'\\n\", outpath);\n        fclose(outfile);\n        return -1;\n    }","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Programs/_freeze_module.c#L177-L213","documentation":"Build-time error from Programs/_freeze_module.c: write_frozen() cannot fopen() the output C file for writing ('w' mode). The directory does not exist, permissions are wrong, or the path is invalid, so the marshalled frozen module cannot be emitted and the build step fails.","triggerScenarios":"Running `_freeze_module name in.py out.c` where out.c's parent directory does not exist or is read-only; builds where the output dir (e.g. build/ generated objects) was not created first; out-of-tree builds with bad relative paths.","commonSituations":"Manually invoking _freeze_module without mkdir of the target dir; a clean-ish tree missing the build output directory; permission issues when building as a different user than the checkout owner.","solutions":["Create the output directory first: `mkdir -p $(dirname out.c)` then rerun","Fix permissions on the target directory (chown/chmod) so the build user can write","Use the Makefile targets instead of invoking _freeze_module by hand so paths are set up correctly"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"# shell: create and permission the output dir before freezing\n# mkdir -p \"$(dirname \"$OUT\")\" && [ -w \"$(dirname \"$OUT\")\" ] || exit 1","typeGuard":null,"tryCatchPattern":null,"preventionTips":["mkdir -p the output directory as part of the build rule","Build as the user who owns the checkout","Prefer Makefile targets over hand-run freezer commands so directory setup is included"],"tags":["cpython","build","freeze-module","permissions","output-path"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}