File.open

File.open(*, start_byte=0, end_byte=None) io.BytesIOarrow-up-right

Read the file as a BytesIO stream. To convert to a TextIO stream, wrap with io.TextIOWrapperarrow-up-right. 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 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.

Do 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:

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.BytesIOarrow-up-right

Examples

import redivis
from io import TextIOWrapper

# 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() as f:
  f.read(100) # read 100 bytes

with TextIOWrapper(file.open()) as f:
  f.readline() # read first line
  
# We can use streams to work with files similarly 
# to if they were on disk, e.g.:  

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?