# File

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

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.&#x20;

## Constructors

<table data-header-hidden><thead><tr><th width="368">Method</th><th>Description</th></tr></thead><tbody><tr><td><a href="/pages/kQT1Kj1hIJ5h93je8J0E"><strong><code>Table$file</code></strong></a>(path)</td><td>Reference a file within a table.</td></tr><tr><td><a href="/pages/q85dGevE4gWHYQAB6cp8"><strong><code>Query$file</code></strong></a>(path)</td><td>Reference a file within query results.</td></tr><tr><td><a href="/pages/ko984apgZa0RNDgYCAAL"><strong><code>Directory$get</code></strong></a>(path)</td><td>Get a file (or directory) within a directory.</td></tr><tr><td><a href="/pages/M71WneRMzhKsiHOgQPjN"><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="/pages/L6MXyhLtQHtpDMONAVIR"><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="/pages/Iom6bLQoStWzWG15GhPg"><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

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

t <- redivis$table("demo.example_data_files:yz1s:v1_3.example_file_types:4c10")

text_file <- t$file("pandas_core.py")
con <- text_file$open()
readLines(con)

binary_file <- t$file("bogota.tiff")
con <- binary_file$open("rb")
readBin(con)

file_contents <- text_file$read(as_text=TRUE) # Read all contents directly to memory

binary_file$download() # download to current working directory

# You can also use R's native open()
con <- open(redivis$table("table_ref")$file("filename"), "rb")
```

## Fields

| **`directory`**  | A reference to the associated [Directory](/api/client-libraries/redivis-r/reference/directory.md) 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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| **`path`**       | The full path of the file                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| **`query`**      | A reference to the [Query](/api/client-libraries/redivis-r/reference/query.md) from which this file was loaded from. Either this or `table` will be present.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| **`properties`** | <p>A named list 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> (POSIXct 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](/api/client-libraries/redivis-r/reference/table.md) 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="/pages/MuCA9bcqQVXjVmPu1g31"><strong><code>file$download</code></strong></a>(path[, ...])</td><td>Download the file.</td></tr><tr><td><a href="/pages/UfEiopq5Qs0MUnAiTcVo"><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="/pages/HbFsrTp1hW927MvH7dnn"><strong><code>file$open</code></strong></a>([mode])</td><td>Return an open connection to the file</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.redivis.com/api/client-libraries/redivis-r/reference/file.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
