Skip to content

file

pyDataverse.dataverse.file

Represents a Dataverse file instance, including its metadata and data access methods.

Provides convenient methods to retrieve, update, and access file contents, as well as utilities for tabular file operations.

Attributes

NameTypeDescription
identifierintThe unique integer identifier for the Dataverse file.
datasetDatasetThe parent dataset this file belongs to.

Examples

>>> file = dataset.files["data/file.csv"]
>>> print(file.metadata.data_file.filesize)
>>> df = file.open_tabular()
>>> file.download("local_copy.csv")

Attributes

NameTypeDescription
idintThe database ID of the file.
persistent_idstrThe persistent ID of the file.
metadataFileInfoReturns the full FileInfo metadata for this file.
content_typestrThe content type of the file.
sizeintThe size of the file in bytes.
pathstrThe file’s path within the dataset, e.g. “data/table.csv” or “myfile.txt”.
is_tabularboolReturns True if the file is a tabular file, False otherwise.
tabular_schemaDict[str, str]Get the schema (column names and data types) of a tabular file.
is_restrictedboolReturns True if the file is restricted, False otherwise.
urlstrThe URL of the file.

Methods

update_metadata(self, description: Optional[str] = None, categories: Optional[List[str]] = None, prov_freeform: Optional[str] = None, restrict: Optional[bool] = None)

Update the file’s metadata in Dataverse.

Parameters

NameTypeDescription
filenameOptional[str]New name for the file (optional).
directory_labelOptional[str]Directory label (for virtual paths) (optional).
descriptionOptional[str]File description (optional). (default: None)
categoriesOptional[List[str]]List of category strings (optional). (default: None)
prov_freeformOptional[str]Prov freeform (optional). (default: None)
restrictOptional[bool]Restrict the file (optional). (default: None)

Returns

TypeDescription
Updated file metadata object.
open_in_browser(self) -> None

Open the file in the default browser.

open(self, mode: Literal['r', 'rb'] = 'r') -> DataverseFileReader

Open the file for binary reading with a streaming reader.

Returns

TypeDescription
DataverseFileReaderFile-like object supporting .read() chunks.
open_tabular(self, no_header: bool = False, **kwargs) -> pd.DataFrame

Open a tabular Dataverse file (e.g., CSV, TSV) and load it as a pandas DataFrame.

Parameters

NameTypeDescription
no_headerboolIf True, do not treat the first row as column names (default False). (default: False)
**kwargsAdditional arguments forwarded to pandas.read_csv. (default: {})

Returns

TypeDescription
pd.DataFramepd.DataFrame: DataFrame containing file contents.
stream_tabular(self, chunk_size: int = 10000, no_header: bool = False, **kwargs) -> Iterator[pd.DataFrame]

Read a tabular file as a stream of pandas DataFrames (chunks) for large files.

Parameters

NameTypeDescription
chunk_sizeintNumber of rows per DataFrame chunk (default 10,000). (default: 10000)
no_headerboolIf True, do not treat the first row as column names. (default: False)
**kwargsAdditional keyword arguments to pandas.read_csv. (default: {})
download(self, path: Union[str, Path], chunk_size: int = 8192) -> None

Download the file from Dataverse and save it to a local file.

Parameters

NameTypeDescription
pathUnion[str, Path]Local path to save the downloaded file to.
chunk_sizeintNumber of bytes to read per chunk (default 8192). (default: 8192)

Returns

TypeDescription
NoneNone
replace(self, file: Union[str, Path, IO[str], IO[bytes]], description: Optional[str] = None, categories: Optional[List[str]] = None, restrict: Optional[bool] = None) -> None

Replace the file with a new file.

Parameters

NameTypeDescription
fileUnion[str, Path, IO[str], IO[bytes]]The new file to replace the current file with.
descriptionOptional[str]The new description for the file. (default: None)
categoriesOptional[List[str]]The new categories for the file. (default: None)
restrictOptional[bool]Whether to restrict the file. (default: None)

Returns

TypeDescription
NoneNone
delete(self) -> None

Delete the file from Dataverse.