io
Table of Contents
open
- buffering
- 0
- unbuffered (read and write are one system call and can return short)
- 1
- line buffered (only usable if universalnewlines=True i.e., in a text mode)
- any other positive value
- use a buffer of approximately that size
- negative bufsize (the default)
the default buffering policy as follows:
- binary files use
io.DEFAULT_BUFFER_SIZE - interactive text files(files for which
isatty()returnsTrue) use line buffering. - In other words, even
sys.stdoutcan be fully buffered if it runs as a background proces
- binary files use
- Returns
readline
f.readline()reads a single line from the file- A newline character (
\n) is left at the end of the string - Handles whether or not the last line has a newline character
- A blank line is represented by
\n
An useful idiom is:
When the buffer is flushed? discussion
- when the buffer gets too small for all pending data;
- when
flush()is called; - when a
seek()is requested (forBufferedRandomobjects); - when the
BufferedWriterobject is closed or destroyed.