File
class File
An interface for working with files on Redivis. When the constructor is called directly, a file_id must be provided. Also returned by listing files associated with a particular table or query result.
Constructors
query.list_files([max_results, *, ...])
List files contained within a query result. The query result must contain at least one file_id variable.
redivis.file(file_id)
Construct a reference to a file based on its globally unique id.
table.list_files([max_results, *, ...])
List files contained within a file index table. The table must contain at least one file_id variable.
Examples
f = redivis.file("4c10-d8rqhswh8.zz7AB9NJB2ctNpDGKbRD7w")
download_path = f.download("./my-downloads")
with open (download_path, "r") as f:
# Do stuff with the file!f = redivis.file("4c10-bk27tqmet.rkgXgUlmnJrEFFC6oWAqqA")
# set as_text to False to read as Bytes
file_content_string = f.read(as_text=True)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()))
Attributes
id
The globally unique identifier for the file, as a string.
query
A reference to the query from which this file was loaded from. Will only be populated if constructed via Query.list_files
properties
A dict containing the API resource representation of the workflow. This will be fully populated after calling File.get(), containing the following properties:
id
str: The globally unique id of the filename
str: The name of the file, including any extensionssize
int: The size of the file, in bytescontentType
str: The MIME type associated with the file's extension, when availablemd5
str: The md5 checksum of the file, as a base64 string
table
A reference to the table from which this file was loaded from. Will only be populated if constructed via Table.list_files.
Methods
file.download(path[, ...])
Download the file.
file.get()
Get file metadata, after which file.properties will be fully populated.
file.read(*[, as_text, start_byte, end_byte])
Read the file contents into memory, either as bytes (the default) or as a string if as_text=True.
file.stream(*, [start_byte])
Read the file as a BytesIO stream, similarly to if it was located on disk.
Last updated
Was this helpful?