{"record":{"id":"00ff4bab9c2e3325","repo":"python/cpython","slug":"file-or-stream-is-not-readable","errorCode":null,"errorMessage":"File or stream is not readable.","messagePattern":"File or stream is not readable\\.","errorType":"exception","errorClass":"UnsupportedOperation","httpStatus":null,"severity":"error","filePath":"Lib/_pyio.py","lineNumber":448,"sourceCode":"    def _checkSeekable(self, msg=None):\n        \"\"\"Internal: raise UnsupportedOperation if file is not seekable\n        \"\"\"\n        if not self.seekable():\n            raise UnsupportedOperation(\"File or stream is not seekable.\"\n                                       if msg is None else msg)\n\n    def readable(self):\n        \"\"\"Return a bool indicating whether object was opened for reading.\n\n        If False, read() will raise OSError.\n        \"\"\"\n        return False\n\n    def _checkReadable(self, msg=None):\n        \"\"\"Internal: raise UnsupportedOperation if file is not readable\n        \"\"\"\n        if not self.readable():\n            raise UnsupportedOperation(\"File or stream is not readable.\"\n                                       if msg is None else msg)\n\n    def writable(self):\n        \"\"\"Return a bool indicating whether object was opened for writing.\n\n        If False, write() and truncate() will raise OSError.\n        \"\"\"\n        return False\n\n    def _checkWritable(self, msg=None):\n        \"\"\"Internal: raise UnsupportedOperation if file is not writable\n        \"\"\"\n        if not self.writable():\n            raise UnsupportedOperation(\"File or stream is not writable.\"\n                                       if msg is None else msg)\n\n    @property\n    def closed(self):","sourceCodeStart":430,"sourceCodeEnd":466,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pyio.py#L430-L466","documentation":"Raised by IOBase._checkReadable() as io.UnsupportedOperation when read() (and readlines/peek-style reads) is called on a stream whose readable() returns False — i.e. a stream opened write- or append-only. It is the runtime enforcement of the mode flags the file was opened with.","triggerScenarios":"open('out.txt', 'w').read(); reading from the stdin pipe of a subprocess (p.stdin.read()); calling .read() on a GzipFile opened 'wb'; helper functions that 'verify' by reading back what they just wrote to a write-mode handle.","commonSituations":"Read-after-write verification on the same handle without reopening; passing a write handle to a logging/tee helper that reads; mode computed wrongly (e.g. 'w' instead of 'r+') so an intended read-write workflow gets a write-only stream.","solutions":["Open for read/write with 'r+' or 'w+' if you must read and write the same handle (mind buffering: flush between write and read).","Verify writes by reopening: close, then open(path, 'rb') and read back.","Guard with if stream.readable(): before read attempts in generic code.","Check the mode string you computed — write-only modes are 'w', 'a', 'x' (and their 'b' forms)."],"exampleFix":"// before\nwith open('out.txt', 'w') as f:\n    f.write(payload)\n    f.seek(0)\n    check = f.read()        # UnsupportedOperation: not readable\n\n// after\nwith open('out.txt', 'w') as f:\n    f.write(payload)\nwith open('out.txt', 'r') as f:\n    check = f.read()        # reopen for reading","handlingStrategy":"type-guard","validationCode":"if stream.readable():\n    data = stream.read(n)\nelse:\n    raise ValueError(f'{stream!r} is not open for reading')","typeGuard":"def is_readable(stream) -> bool:\n    probe = getattr(stream, 'readable', None)\n    return callable(probe) and probe()","tryCatchPattern":"import io\n\ntry:\n    data = f.read()\nexcept io.UnsupportedOperation:\n    # opened 'w'/'a' — reopen for reading if verification was intended\n    with open(f.name, 'rb') as rf:\n        data = rf.read()","preventionTips":["Use 'r+'/'w+' when a workflow legitimately reads and writes one handle; flush between directions.","Verify written data by reopening the file, not by reading the write-mode handle.","Assert the computed mode string before open() in mode-building code."],"tags":["io","stream","read","mode","unsupported-operation"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}