# File

## *class* <mark style="color:purple;">File</mark>

An interface for working with raw files stored on Redivis.

{% hint style="info" %}
Redivis automatically registers itself with [fsspec](https://filesystem-spec.readthedocs.io/en/latest/) on import. If you are working with an fsspec compatible library, you can use the Redivis file URI scheme to reference files directly:

```python
with fsspec.open("redivis://table_reference/path/to/file") as f:
    """ Do great things """
```

{% endhint %}

## Constructors

<table data-header-hidden><thead><tr><th width="368">Method</th><th>Description</th></tr></thead><tbody><tr><td><a href="table/table.file"><strong><code>Table.file</code></strong></a>(path)</td><td>Reference a file within a table.</td></tr><tr><td><a href="query/query.file"><strong><code>Query.file</code></strong></a>(path)</td><td>Reference a file within a query result</td></tr><tr><td><a href="directory/directory.get"><strong><code>Directory.get</code></strong></a>(path)</td><td>Get a file (or directory) within a directory.</td></tr><tr><td><a href="directory/directory.list"><strong><code>Directory.list</code></strong></a>([max_results, *, ...])</td><td>List files (and/or directories) within a directory</td></tr><tr><td><a href="query/query.list_files"><strong><code>Query.list_files</code></strong></a>([max_results, *, ...])</td><td>List files contained within a query result. The query result must contain at least one file_id variable.</td></tr><tr><td><a href="table/table.list_files"><strong><code>Table.list_files</code></strong></a>([max_results, *, ...])</td><td>List files contained within a file index table. The table must contain at least one file_id variable.</td></tr></tbody></table>

## Examples

```python
import redivis
from io import TextIOWrapper
from PIL import Image

# 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")
text_file = table.file("pandas_core.py")
image_file = table.file("bogota.tiff")

## Read file contents
str = text_file.read(as_text=True)
bytes = image_file.read()

## Open the file, as if it was on the filesystem
with file.open("rb") as f:
  f.read(100) # read 100 bytes

with file.open() as f:
  f.readline() # read first line
  
# Tools that integrate with fsspec can open Redivis URIs:
pystac.Catalog.from_file("redivis://table_ref/stac/catalog.json")
  
Image.open(table.file("bogota.tiff")) # PIL will automatically call open() on the file
  
## Download the file  
image_file.download("./path") # will be downloaded as ./path/bogota.tiff
text_file.download("./path/renamed.txt") # will be downloaded as ./path/renamed.txt
```

## Attributes

| **`directory`**  | A reference to the associated [Directory](https://docs.redivis.com/api/client-libraries/redivis-python/reference/directory) for this file.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`id`**         | The globally unique identifier for the file, as a string.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| **`name`**       | The name of the file as a string, without any directory subpaths as present. Same as `file.path.name`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| **`path`**       | The full path of the file, as a pathlib.Path                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| **`query`**      | A reference to the [Query](https://docs.redivis.com/api/client-libraries/redivis-python/reference/query) from which this file was loaded from. Either this or `table` will be present.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| **`properties`** | <p>A dict containing properties associated with the file. This will always contain the following properties, derived from the file's original index table:<br></p><ul><li><strong><code>file\_id</code></strong> (str): The globally unique id of the file</li><li><strong><code>file\_name</code></strong> (str): The full name of the file, including any extensions</li><li><strong><code>size</code></strong> (int): The size of the file, in bytes</li><li><strong><code>added\_at</code></strong> (datetime): When the file was initially uploaded to Redivis</li><li><strong><code>md5\_hash</code></strong> (str): The md5 checksum of the file, as a base64 string</li></ul><p>Additionally, if the file was loaded from a table or query with additional variables, those variables' values will exist in properties.</p> |
| **`table`**      | A reference to the [Table](https://docs.redivis.com/api/client-libraries/redivis-python/reference/table) from which this file was loaded from.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |

## Methods

<table data-header-hidden><thead><tr><th width="421">Name</th><th>Description</th></tr></thead><tbody><tr><td><a href="file/file.download"><strong><code>file.download</code></strong></a>(path[, ...])</td><td>Download the file.</td></tr><tr><td><a href="file/file.read"><strong><code>file.read</code></strong></a>(*[, as_text, start_byte, end_byte])</td><td>Read the file contents into memory, either as bytes (the default) or as a string if as_text=True.</td></tr><tr><td><a href="file/file.open"><strong><code>file.open</code></strong></a>(*, [start_byte, end_byte])</td><td>Read the file as a BytesIO stream, as if it was located on disk.</td></tr></tbody></table>
