Python Glossary Русский

all Python 3Exceptionsexception

io.UnsupportedOperation

module exceptions

The stream does not support the operation (e.g. writing to a file opened for reading); a subclass of OSError and ValueError.

Syntax

raise io.UnsupportedOperation

This is not about bad data but about the mode a stream was opened in: writing to a file opened for reading, reading from a write-only file, or seeking on a non-seekable stream such as stdin or a pipe. Rather than catching it, ask the stream first via writable(), readable() or seekable(). Note that it inherits from both OSError and ValueError, so an except ValueError block will swallow it, which surprises people.

Examples

import io
try:
    raise io.UnsupportedOperation
except io.UnsupportedOperation as e:
    print(type(e).__name__)   # → UnsupportedOperation

See also

Open in the full glossarydocs.python.orgReport an inaccuracy