io.FileIO
Prev: io.BytesIO | Up: io | Next: io.IncrementalNewlineDecoder
Rel: io.BlockingIOError, io.BufferedIOBase, io.BufferedRandom, io.BufferedReader
Ref: docs.python.org — io.FileIO
Tags: #public #python3 #stdlib #io
Import
import io
from io import FileIO
Signature & Overview
io.FileIO(file, mode='r', closefd=True, opener=None)
Open a file.
Methods & Attributes
FileIO.close(self, /)— Close the file.FileIO.fileno(self, /)— Return the underlying file descriptor (an integer).FileIO.flush(self, /)— Flush write buffers, if applicable.FileIO.isatty(self, /)— True if the file is connected to a TTY device.FileIO.read(self, size=-1, /)— Read at most size bytes, returned as bytes.FileIO.readable(self, /)— True if file was opened in a read mode.FileIO.readall(self, /)— Read all data from the file, returned as bytes.FileIO.readinto(self, buffer, /)— Same as RawIOBase.readinto().FileIO.readline(self, size=-1, /)— Read and return a line from the stream.FileIO.readlines(self, hint=-1, /)— Return a list of lines from the stream.FileIO.seek(self, pos, whence=0, /)— Move to new file position and return the file position.FileIO.seekable(self, /)— True if file supports random-access.FileIO.tell(self, /)— Current file position.FileIO.truncate(self, size=None, /)— Truncate the file to at most size bytes and return the truncated size.FileIO.writable(self, /)— True if file was opened in a write mode.FileIO.write(self, b, /)— Write buffer b to file, return number of bytes written.FileIO.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) / "raw.bin"
with io.FileIO(p, "w") as f:
f.write(b"raw")
assert p.read_bytes() == b"raw"
Gotchas
- Unbuffered OS-level binary I/O: Equivalent to
open(path, "wb", buffering=0). A singleread()/write()maps to one syscall and may process fewer bytes than requested.
REPL Inspect
from io import FileIO
help(FileIO)
Linked from
Related notes
- tuts/python3/stdlib_module/io/BytesIO Linked here; links to this page; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/IncrementalNewlineDecoder 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