On this page
io.BufferedWriter
Prev: io.BufferedRWPair | Up: io | Next: io.BytesIO
Rel: io.BlockingIOError, io.BufferedIOBase, io.BufferedRandom, io.BufferedReader
Ref: docs.python.org — io.BufferedWriter
Tags: #public #python3 #stdlib #io
Import
import io
from io import BufferedWriter
Signature & Overview
io.BufferedWriter(raw, buffer_size=131072)
A buffer for a writeable sequential RawIO object.
Methods & Attributes
BufferedWriter.close(self, /)—BufferedWriter.closemethod/property.BufferedWriter.detach(self, /)—BufferedWriter.detachmethod/property.BufferedWriter.fileno(self, /)—BufferedWriter.filenomethod/property.BufferedWriter.flush(self, /)—BufferedWriter.flushmethod/property.BufferedWriter.isatty(self, /)—BufferedWriter.isattymethod/property.BufferedWriter.read(self, size=-1, /)— Read and return up to n bytes.BufferedWriter.read1(self, size=-1, /)— Read and return up to size bytes, with at most one read() call to the underlying raw stream.BufferedWriter.readable(self, /)— Return whether object was opened for reading.BufferedWriter.readinto(self, buffer, /)—BufferedWriter.readintomethod/property.BufferedWriter.readinto1(self, buffer, /)—BufferedWriter.readinto1method/property.BufferedWriter.readline(self, size=-1, /)— Read and return a line from the stream.BufferedWriter.readlines(self, hint=-1, /)— Return a list of lines from the stream.BufferedWriter.seek(self, target, whence=0, /)—BufferedWriter.seekmethod/property.BufferedWriter.seekable(self, /)—BufferedWriter.seekablemethod/property.BufferedWriter.tell(self, /)—BufferedWriter.tellmethod/property.BufferedWriter.truncate(self, pos=None, /)—BufferedWriter.truncatemethod/property.BufferedWriter.writable(self, /)—BufferedWriter.writablemethod/property.BufferedWriter.write(self, buffer, /)—BufferedWriter.writemethod/property.BufferedWriter.writelines(self, lines, /)— Write a list of lines to stream.
Usage
import io
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "out.bin"
with open(p, "wb") as f:
assert isinstance(f, io.BufferedWriter)
f.write(b"chunk")
assert p.read_bytes() == b"chunk"
Gotchas
- Buffers writes in memory until full, flushed, or closed: Always use a
withblock or callf.flush()so buffered bytes reach disk.
REPL Inspect
from io import BufferedWriter
help(BufferedWriter)
Linked from
Related notes
- tuts/python3/stdlib_module/io/BufferedRWPair Linked here; links to this page; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/BytesIO Linked here; links to this page; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/io/BlockingIOError Linked here; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/BufferedIOBase Linked here; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/BufferedRandom Linked here; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/BufferedReader Linked here; shared tags: #io, #stdlib
- tuts/c/stdlib/stdio_and_file_io shared tags: #io, #stdlib
- tuts/cpp/stdlib/io_and_filesystem shared tags: #io, #stdlib
- tuts/cpp/stdlib/io_and_filesystem/format_and_print shared tags: #io, #stdlib