File.stream

File.stream() io.BytesIO

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

Returns:

io.BytesIO

Examples

from io import TextIOWrapper

f = redivis.file("rnwk-acs3famee.pVr4Gzq54L3S9pblMZTs5Q")

with f.stream() as f_stream:
  f_stream.read(100) # read 100 bytes

with TextIOWrapper(f.stream()) as f_stream:
  f_stream.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
# References the file bogota.tiff at https://redivis.com/datasets/yz1s-d09009dbb/files/4c10-d8rqhswh8.zz7AB9NJB2ctNpDGKbRD7w
file = redivis.file("4c10-d8rqhswh8.zz7AB9NJB2ctNpDGKbRD7w")
# Pass the file as a bytesIO stream to PIL to load as an image
print(Image.open(file.stream()))

Last updated