File.open

File.open(mode="r", encoding="utf-8", *, start_byte=0, end_byte=None) io.TextIOarrow-up-right | io.BytesIOarrow-up-right

Read the file. Note that by default the underlying stream won't be closed until fully consumed – it is strongly recommended to make the request within a with statement to ensure it’s always closed.

Note that some methods will automatically call open(), and you can pass the file directly without opening it.

circle-info

Open files return a true io.BytesIO / io.TextIO interface, as if the file were opened on a local disk. This allows for the file's contents to be incrementally read as needed, rather than loading the full file to memory first.

Note that seek() operations will be higher latency than files stored on-disk, as the file is being incrementally read over the network. If you are performing numerous small, partial reads, it may make sense to first download the file to disk.

Parameters:

mode : "r" | "rb" | "rt", default "r" Whether to read the file as text or binary. Similar to default python file behavior, defaults to text mode ("r"). "rb" is for binary mode. "rt" is an alias for "r".

encoding : str, default "utf-8" Only relevant if mode is "r" / "rt". The text encoding, defaults to uft-8.

start_byte : int, default 0 The offset byte at which to start reading the file. Allows for reading part of a file. If not specified, the file will be read from the start.

end_byte : int, default None The byte at which to stop reading the file, inclusive. Allows for streaming part of a file. If not specified, the file will be streamed to the end.

Returns:

io.TextIOarrow-up-right | io.BytesIOarrow-up-right

Examples

import redivis

# See https://redivis.com/datasets/yz1s-d09009dbb/files for example data

table = redivis.table("demo.example_data_files:yz1s:v1_3.example_file_types:4c10")
file = table.file("pandas_core.py")

with file.open("rb") as f:
  f.read(100) # read 100 bytes

with file.open() as f:
  f.readline() # read first line

from PIL import Image

# Note that PIL will automatically call open() on the file
Image.open(table.file("bogota.tiff"))

Last updated

Was this helpful?