Skip to main content

io

The io module is the implementation of open() and the family of stream classes Python uses for every kind of file-like object. It is a three-layer stack:

LayerBase classPurpose
RawRawIOBaseUnbuffered byte I/O on a file descriptor.
BufferedBufferedIOBaseByte buffering on top of a raw stream.
TextTextIOBaseCodec on top of a buffered byte stream.

Source-of-record: Modules/_io/*, Lib/io.py, Lib/_pyio.py, io docs.

open and helpers

open(file, mode='r', buffering=-1, encoding=None, errors=None,
newline=None, closefd=True, opener=None)
Mode charEffect
rRead (default).
wWrite, truncate.
xExclusive create.
aAppend.
bBinary.
tText (default).
+Read and write.

Helpers:

FunctionReturns
io.open_code(path)Opens a .py source file for the import system.
io.text_encoding(encoding, stacklevel=2)Resolves the default text encoding, warning if needed.
io.IOBase.__del__Closes on collection.

IOBase interface

All stream types implement at least:

MethodRole
close()Flush and release resources.
closedPredicate.
fileno()OS file descriptor; OSError if none.
flush()Push buffered data.
isatty()Predicate.
readable()Predicate.
readline(size=-1)One line.
readlines(hint=-1)All lines.
seek(offset, whence=SEEK_SET)Reposition.
seekable()Predicate.
tell()Position.
truncate(size=None)Truncate to size.
writable()Predicate.
writelines(lines)Many writes.
__enter__ / __exit__Context manager protocol.
__iter__ / __next__Iterates lines.

SEEK_SET=0, SEEK_CUR=1, SEEK_END=2.

Raw streams

ClassWrapsNotes
FileIOOS file descriptorRawIOBase over read/write.
BytesIOIn-memory bytes bufferBinary stream backed by bytes.

FileIO(path, mode='r', closefd=True, opener=None) accepts 'r', 'w', 'x', 'a', '+'.

Buffered streams

ClassWrapsUse
BufferedReaderRaw read streamRead buffering.
BufferedWriterRaw write streamWrite buffering, flushes on close.
BufferedRandomRaw rw streamBidirectional buffering.
BufferedRWPairTwo raw streamsPipe pairs.

Buffered API additions:

MethodRole
read(size=-1)Up to size bytes; full buffer flush.
read1(size=-1)One underlying read.
readinto(b)Fill b.
readinto1(b)One underlying read into b.
peek(size=0)Peek without advancing.
write(b)Returns bytes written.
rawThe wrapped raw stream.

Default buffer size is DEFAULT_BUFFER_SIZE = 8192.

Text streams

ClassWrapsNotes
TextIOWrapperBuffered binaryThe encoding layer applied to files.
StringIOIn-memory strText stream backed by str.
IncrementalNewlineDecoderbytes -> strNewline translation.

TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False, write_through=False).

AttributeMeaning
encodingCodec name.
errorsCodec error handler.
newlineTranslation: '', '\\n', '\\r', '\\r\\n'.
line_bufferingFlush on each newline.
write_throughPass writes straight through, no buffering.
bufferUnderlying buffered byte stream.

reconfigure(encoding=None, errors=None, newline=None, line_buffering=None, write_through=None) mutates settings.

Exceptions

ExceptionTrigger
BlockingIOErrorNon-blocking I/O would block.
UnsupportedOperationOSError + ValueError; raised by base classes when an op isn't valid.

Gopy status

AreaState
open and mode parsingComplete.
FileIO, BytesIO, StringIOComplete.
Buffered classesComplete.
TextIOWrapper and newline policyComplete.
Non-blocking and locking semanticsComplete; uses Go's os package under the hood.

Reference

  • CPython 3.14: io module.
  • Modules/_io/. C side.
  • Lib/io.py, Lib/_pyio.py. Python side.
  • module/io/. gopy port.