{"record":{"id":"97987fab47a4e9d3","repo":"deepfakes/faceswap","slug":"permissionerror-while-reading-filename","errorCode":null,"errorMessage":"PermissionError while reading: {filename}","messagePattern":"PermissionError while reading: (.+?)","errorType":"exception","errorClass":"PermissionError","httpStatus":null,"severity":"error","filePath":"lib/image.py","lineNumber":276,"sourceCode":"    >>> width = metadata[\"width]\n    >>> height = metadata[\"height\"]\n    >>> faceswap_info = metadata[\"itxt\"]\n    \"\"\"\n    retval = {}\n    if os.path.splitext(filename)[-1].lower() != \".png\":\n        # Get the dimensions directly from the image for non-png\n        logger.trace(  # type:ignore[attr-defined]\n            \"Non png found. Loading file for dimensions: '%s'\",\n            filename)\n        img = cv2.imread(filename)\n        assert img is not None\n        retval[\"height\"], retval[\"width\"] = img.shape[:2]\n        return retval\n    with open(filename, \"rb\") as in_file:\n        try:\n            chunk = in_file.read(8)\n        except PermissionError as exc:\n            raise PermissionError(f\"PermissionError while reading: {filename}\") from exc\n\n        if chunk != b\"\\x89PNG\\r\\n\\x1a\\n\":\n            raise ValueError(f\"Invalid header found in png: {filename}\")\n\n        while True:\n            chunk = in_file.read(8)\n            length, field = struct.unpack(\">I4s\", chunk)\n            logger.trace(  # type:ignore[attr-defined]\n                \"Read chunk: (chunk: %s, length: %s, field: %s\",\n                chunk, length, field)\n            if not chunk or field == b\"IDAT\":\n                break\n            if field == b\"IHDR\":\n                # Get dimensions\n                chunk = in_file.read(8)\n                retval[\"width\"], retval[\"height\"] = struct.unpack(\">II\", chunk)\n                length -= 8\n            elif field == b\"iTXt\":","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/deepfakes/faceswap/blob/f530cb7508ae670f6474f8a7d9c4df94705cf96b/lib/image.py#L258-L294","documentation":"Raised while reading the first 8 bytes of a file that faceswap's PNG dimension parser is probing. The file opened successfully but reading it failed with an OS PermissionError, which faceswap re-raises with the offending filename. This happens inside lib/image.py's PNG header reader (read_dim_batch / dimensions detection path).","triggerScenarios":"Calling the image dimension API on a file whose read permission is denied for the current user (chmod 000, owned by another user, file locked by another process on Windows). Occurs during batch scans of folders containing unreadable files.","commonSituations":"Running extraction/training over a dataset copied from another machine with restrictive permissions, files created by a root/service user, security software locking files, or read-only mounts.","solutions":["Fix the file permissions: chmod u+r '<filename>' or take ownership of the dataset folder (chown -R).","Exclude or delete the unreadable file from the input folder and rerun.","Run the faceswap process as a user with read access to the dataset location.","On Windows, close applications (or antivirus) holding an exclusive lock on the file."],"exampleFix":"# before (dataset contains unreadable file)\n$ python faceswap.py extract -i /data/src -o /data/faces\n# PermissionError while reading: /data/src/locked.png\n\n# after\n$ sudo chown -R $(whoami) /data && chmod -R u+rw /data\n$ python faceswap.py extract -i /data/src -o /data/faces","handlingStrategy":"validation","validationCode":"import os\n\nreadable = os.access(filename, os.R_OK)\nif not readable:\n    print(f'skip unreadable file: {filename}')","typeGuard":null,"tryCatchPattern":"try:\n    dims = read_image_meta(filename)\nexcept PermissionError as err:\n    logger.warning('skipping unreadable %s: %s', filename, err)\n    continue","preventionTips":["Run os.access(path, os.R_OK) over input folders before batch jobs.","Keep dataset ownership aligned with the user running faceswap.","Avoid mixing files from different users/permissions in one input folder."],"tags":["filesystem","permissions","png","io"],"backgroundTag":null,"analyzedSha":"f530cb7508ae670f6474f8a7d9c4df94705cf96b","analyzedAt":"2026-08-15T02:59:26.626Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}