# Directory

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

Any table or query result on Redivis that contains file ids can be read as a directory, where the file names are parsed according to any `/` separators, and a directory tree is created.&#x20;

This virtual directory can then be used to navigate, download, and even mount a given file tree.

## Constructors

<table data-header-hidden><thead><tr><th width="319">Method</th><th>Description</th></tr></thead><tbody><tr><td><a href="directory/directoryusdget"><strong><code>Directory$get</code></strong></a>(path)</td><td>Returns a subdirectory within a directory.</td></tr><tr><td><a href="table/tableusdto_directory"><strong><code>Query$to_directory</code></strong></a>(name)</td><td>Load query results that contain a <code>file_id</code> variable as a directory.</td></tr><tr><td><a href="table/tableusdto_directory"><strong><code>Table$to_directory</code></strong></a>(name)</td><td>Load any table contains a <code>file_id</code> variable as a directory.</td></tr></tbody></table>

## Examples

{% tabs %}
{% tab title="Basics" %}

```r
dir <- redivis$table("table_ref")$to_directory()
print(dir$list()) # list files + directories within this directory
all_files <- dir$list(mode="files", recursive=True)

file <- dir$get("path/to/file.txt") # Will return None if doesn't exist
subdir <- dir$get("path/to/subdir")
```

{% endtab %}

{% tab title="Mount & Download files" %}

```r
dir <- redivis$table("table_ref")$to_directory()

# Download all files to the provided path
dir$download("/download/path")

# dir$mount() behaves the same as dir$download() was called (all files appear on disk)
# However, this doesn't actually download the files until they're needed, 
#    and as such is much faster, particularly when all files may not need to be read.
dir$mount("/download/path")
```

{% endtab %}
{% endtabs %}

## Attributes

| **`path`**  | <p>The path of this directory relative to the root directory in the current tree. <br><br>The path of the top-level directory (that is, any directory returned from table / query <code>$to\_directory()</code> ) will always be <code>"/"</code></p> |
| ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`query`** | A reference to the [Query](https://docs.redivis.com/api/client-libraries/redivis-r/reference/query) that constructed this directory. Will be `None` if the directory is derived from a table.                                                         |
| **`table`** | A reference to the [Table](https://docs.redivis.com/api/client-libraries/redivis-r/reference/table) that constructed this directory. Will be `None` if the directory is derived from a query.                                                         |

## Methods

<table data-header-hidden><thead><tr><th width="441"></th><th></th></tr></thead><tbody><tr><td><a href="directory/directoryusddownload"><strong>Directory$download</strong></a>([path, overwrite, ...])</td><td>Downloads files in the directory to the specified path.</td></tr><tr><td><a href="directory/directoryusdget"><strong>Directory$get</strong></a>(path)</td><td>Get a file or directory relative to this directory</td></tr><tr><td><a href="directory/directoryusdlist"><strong>Directory$list</strong></a>([max_results, mode, recursive])</td><td>List the contents of a directory</td></tr><tr><td><a href="directory/directoryusdmount"><strong>Directory$mount</strong></a>([path, foreground])</td><td>Mount the directory as a volume at the provided path, allowing files to be lazily loaded as they're accessed.</td></tr><tr><td><a href="directory/directoryusdunmount"><strong>Directory$unmount</strong></a>([path, foreground])</td><td>Unmounts a previously mounted directory.</td></tr></tbody></table>
