AsyncFile#
- class opendal.file.AsyncFile#
An async file-like object for reading and writing data.
Created by the open method of the AsyncOperator class.
- 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:
- coroutine
An awaitable that returns True if this file is closed.
- read(size=None)#
Read at most size bytes from this file asynchronously.
If size is not specified, read until EOF.
- Parameters:
- sizeint, optional
The maximum number of bytes to read.
- Returns:
- coroutine
An awaitable that returns the bytes read from the stream.
Notes
Fewer bytes may be returned than requested, read in a loop to ensure all bytes are read.
- read_buffered(size)#
Read up to size bytes by combining immediately available data.
A positive-sized read waits for the first bytes, then returns when more data would require waiting, at EOF, or at the requested limit. Keep reading until an empty result to consume the file. A zero-sized read returns empty bytes without consuming data or a pending error.
If a read encounters an error after collecting bytes, it returns those bytes and the next positive-sized read raises the error. A successful seek clears a pending error. Cancelling a pending read preserves the file position and any bytes collected for that read.
- Parameters:
- sizeint
The maximum number of bytes to read. Must be non-negative.
- Returns:
- coroutine
An awaitable that returns the bytes read from the stream.
- readable()#
Whether this file can be read from.
- Returns:
- coroutine
An awaitable that returns True if this file can be read from.
- 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:
- coroutine
An awaitable that returns the current 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:
- coroutine
An awaitable that returns 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:
- coroutine
An awaitable that returns the current absolute position.
- Raises:
- ValueError
If the file is closed.
- writable()#
Whether this file can be written to.
- Returns:
- coroutine
An awaitable that returns True if this file can be written to.
- write(bs)#
Write bytes to this file asynchronously.
- Parameters:
- bsbytes
The bytes to write to the file.
- Returns:
- coroutine
An awaitable that returns the number of bytes written.
- Raises:
- ChecksumMismatch
If checksum verification fails during the write. This error also inherits from OSError.