File#

class opendal.file.File#

A file-like object for reading and writing data.

Created by the open method of the Operator class.

Storage I/O releases the Python GIL while waiting. Concurrent operations on the same file are rejected; use separate files for I/O from multiple threads.

close()#

Close this file.

This also flushes write buffers, if applicable.

Notes

A closed file cannot be used for further I/O operations.

closed#

Whether this file is closed.

Returns:
bool

True if this file is closed.

flush()#

Flush the underlying writer.

Raises:
ChecksumMismatch

If checksum verification fails while flushing. This error also inherits from OSError.

Notes

Passes pending input to the core writer. Storage chunks can remain buffered until more data arrives or close() completes the write. Is a no-op if the file is not writable.

read(size=None)#

Read at most size bytes from this file.

If size is not specified, read until EOF.

Parameters:
sizeint, optional

The maximum number of bytes to read.

Returns:
bytes

The bytes read from this file.

Notes

Fewer bytes may be returned than requested, read in a loop to ensure all bytes are read.

readable()#

Whether this file can be read from.

Returns:
bool

True if this file can be read from.

readinto(buffer)#

Read bytes into a pre-allocated buffer.

Parameters:
bufferbytes | bytearray

A writable, pre-allocated buffer to read into.

Returns:
int

The number of bytes read.

readline(size=None)#

Read one line from this file.

If size is not specified, read until newline.

Parameters:
sizeint, optional

The maximum number of bytes to read.

Returns:
bytes

The bytes read from this file.

Notes

Retains newline characters after each line, unless the file’s last line has no terminating newline.

seek(pos, whence=0)#

Change the position of this file to the given byte offset.

Parameters:
posint

The byte offset (position) to set.

whenceint, optional

The reference point for the offset. 0: start of file (default); 1: current position; 2: end of file.

Returns:
int

The new absolute position.

Notes

Unbounded readable files allow non-negative positions beyond EOF. Reads from those positions return empty bytes when the backing service reports an unsatisfied unbounded range. Other service errors propagate. Explicit bounded views reject positions beyond their range.

seekable()#

Whether this file can be repositioned.

Returns:
bool

True if this file can be repositioned.

Notes

This is only applicable to readable files.

tell()#

Return the current position of this file.

In write mode, the position is the number of bytes accepted by the writer, including buffered bytes.

Returns:
int

The current absolute position.

Raises:
ValueError

If the file is closed.

writable()#

Whether this file can be written to.

Returns:
bool

True if this file can be written to.

write(bs)#

Write bytes to this file.

Parameters:
bsbytes

The bytes to write to the file.

Returns:
int

The number of bytes written.

Raises:
ChecksumMismatch

If checksum verification fails during the write. This error also inherits from OSError.