Skip to content

File

A file-like object for reading and writing data.

Created by the open method of the Operator class.

closed property #

Whether this file is closed.

Returns:

Type Description
bool

True if this file is closed.

close() #

Close this file.

This also flushes write buffers, if applicable.

Notes

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

flush() #

Flush the underlying writer.

Notes

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:

Name Type Description Default
size int

The maximum number of bytes to read.

None
Notes

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

Returns:

Type Description
bytes

The bytes read from this file.

readable() #

Whether this file can be read from.

Returns:

Type Description
bool

True if this file can be read from.

readinto(buffer) #

Read bytes into a pre-allocated buffer.

Parameters:

Name Type Description Default
buffer bytes | bytearray

A writable, pre-allocated buffer to read into.

required

Returns:

Type Description
int

The number of bytes read.

readline(size=None) #

Read one line from this file.

If size is not specified, read until newline.

Parameters:

Name Type Description Default
size int

The maximum number of bytes to read.

None
Notes

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

Returns:

Type Description
bytes

The bytes read from this file.

seek(pos, whence=0) #

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

Parameters:

Name Type Description Default
pos int

The byte offset (position) to set.

required
whence int

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

0

Returns:

Type Description
int

The new absolute position.

seekable() #

Whether this file can be repositioned.

Notes

This is only applicable to readable files.

Returns:

Type Description
bool

True if this file can be repositioned.

tell() #

Return the current position of this file.

Returns:

Type Description
int

The current absolute position.

writable() #

Whether this file can be written to.

Returns:

Type Description
bool

True if this file can be written to.

write(bs) #

Write bytes to this file.

Parameters:

Name Type Description Default
bs bytes

The bytes to write to the file.

required

Returns:

Type Description
int

The number of bytes written.