Skip to main content

Description

Returns the complete contents of a file tracked by DVC or Git. This is a convenience function that reads the entire file at once without requiring a context manager. For Git repositories, HEAD is used unless a rev argument is supplied. The default remote is tried unless a remote argument is supplied.
For large files, consider using dvc.api.open() instead to stream data and avoid 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 (HTTP or SSH)
  • A local file system path
  • None to use the current repository
str
default:"None"
Any Git revision such as a branch name, tag name, commit hash, or DVC experiment name.
  • Defaults to HEAD for Git repositories
  • For local repositories, uses the working directory if not specified
  • Ignored if repo is not a Git repository
str
default:"None"
Name of the DVC remote to use for fetching data. Defaults to the repository’s default remote.For local projects, the cache is checked before the default remote.
str
default:"r"
Mode in which to open the file. Defaults to "r" (read text mode).Only reading modes are supported:
  • "r" - Read text mode (returns str)
  • "rb" - Read binary mode (returns bytes)
str
default:"None"
Text encoding to use (e.g., "utf-8", "latin-1"). Only applicable in text mode (mode="r").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

Union[str, bytes]
The complete contents of the file:
  • Returns str when mode="r" (text mode)
  • Returns bytes when mode="rb" (binary mode)

Raises

exception
Raised when the specified file does not exist in the repository.
exception
Raised when the file is not tracked by DVC.
exception
Raised when a non-read mode is specified.

Examples

Basic Text File Reading

Read Configuration File

Read JSON Metrics

Binary File Reading

Read from Specific Tag

Private Repository with SSH

Read with Custom Encoding

Read NumPy Array

Read from Local Repository

Error Handling

Use Cases

Configuration Loading

Load parameters, configs, or metadata files for experiments.

Small Data Files

Read datasets that fit comfortably in memory.

Model Loading

Load serialized models for inference or evaluation.

Metrics Retrieval

Fetch experiment metrics for analysis and comparison.

Comparison with dvc.api.open()

read() is a convenience wrapper around open() that reads the entire file and returns its contents.

Performance Considerations

read() loads the entire file into memory. For large files (>100MB), use dvc.api.open() to stream data instead.

Best Practices

read() is ideal for configuration files, parameters, and small datasets:
Use text mode for text files and binary mode for binary data:
Remember to parse the returned string/bytes:
Always catch potential exceptions:

open()

Stream files with context manager

get_url()

Get remote storage URL

DVCFileSystem

Low-level file system access