Skip to main content

Description

Opens a DVC-tracked file and returns a file object for streaming. This function must be used as a context manager with the with keyword. Unlike dvc.api.read(), this function streams file contents directly from remote storage, allowing you to process data incrementally without loading the entire file into memory.

Signature

Parameters

str
required
Location and filename of the target file, relative to the root of the repository.
str
default:"None"
Location of the DVC or Git repository. Defaults to the current project (found by walking up from the current working directory).Can be:
  • A URL to a Git repository
  • A local file system path
  • HTTP and SSH protocols are supported
str
default:"None"
Git revision such as a branch name, tag name, commit hash, or DVC experiment name.
  • Defaults to HEAD for Git repositories
  • For local repositories without rev, reads from the working directory
  • Ignored if repo is not a Git repository
str
default:"None"
Name of the DVC remote to use. Defaults to the repository’s default remote.For local projects, the cache is tried before the default remote.
str
default:"r"
Mode in which to open the file. Defaults to "r" (read mode).Only reading modes are supported.
str
default:"None"
Text encoding to use (e.g., "utf-8", "latin-1"). Only applicable in text mode.Mirrors the encoding parameter in Python’s built-in open().
dict
default:"None"
DVC config dictionary to pass to the repository.
dict
default:"None"
Remote configuration dictionary to pass to the repository.

Returns

_OpenContextManager
A context manager that yields a file object. The exact type depends on the mode:
  • Text mode (mode="r"): Returns a text file object
  • Binary mode (mode="rb"): Returns a binary file object
The file object supports standard file operations like read(), readline(), and iteration.

Raises

exception
Raised when the function is used without a context manager (without with statement).
exception
Raised when a non-read mode is specified (e.g., mode="w").
exception
Raised when the specified file does not exist in the repository.
exception
Raised when the file is not tracked by DVC.

Examples

Basic File Reading

Streaming Large Files

Using with Pandas

Binary File (Model Weights)

XML Parsing with SAX

Private Repository Access

Specific Git Revision

Custom Encoding

Using Specific Remote

Use Cases

Streaming Large Files

Process files larger than available RAM by reading incrementally.

Data Pipeline Integration

Load DVC-tracked datasets directly into training or processing pipelines.

Version-Specific Data

Access different versions of data from various branches or experiments.

Remote Data Access

Stream data directly from cloud storage without local downloads.

Comparison with dvc.api.read()

Choose open() for large files or when you need streaming. Use read() for small files when you need the complete content immediately.

Best Practices

The function must be used as a context manager. This ensures proper cleanup:
Use text mode for text files and binary mode for binary data:
For large files, process data incrementally instead of reading everything:
Wrap API calls in try-except blocks for robust error handling:

read()

Read complete file contents in one call

get_url()

Get the remote storage URL

DVCFileSystem

Low-level file system interface