Datachain

TextFile

TextFile is inherited from File with additional methods for working with text files.

TextFile is generated when a DataChain is created from storage, using type="text" param:

import datachain as dc

chain = dc.read_storage("s3://bucket-name/", type="text")

TextFile

TextFile(**kwargs)

Bases: File

DataModel for reading text files.

Source code in datachain/lib/file.py
327
328
329
330
331
332
def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self._catalog = None
    self._caching_enabled: bool = False
    self._download_cb: Callback = DEFAULT_CALLBACK
    self._fs_path_cache: tuple[str, str, str] | None = None

open

open(mode: str = 'r', *, client_config: dict[str, Any] | None = None, **open_kwargs) -> Iterator[Any]

Open the file and return a file-like object. Default to text mode

Source code in datachain/lib/file.py
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
@contextmanager
def open(
    self,
    mode: str = "r",
    *,
    client_config: dict[str, Any] | None = None,
    **open_kwargs,
) -> Iterator[Any]:
    """Open the file and return a file-like object.
    Default to text mode"""
    with super().open(
        mode=mode, client_config=client_config, **open_kwargs
    ) as stream:
        yield stream

read

read(**open_kwargs)

Return file contents as text (default mode for TextFile).

Source code in datachain/lib/file.py
1165
1166
1167
def read(self, **open_kwargs):
    """Return file contents as text (default mode for TextFile)."""
    return self.read_text(**open_kwargs)

read_text

read_text(**open_kwargs)

Return file contents as text.

**open_kwargs : Any Extra keyword arguments forwarded to open() (e.g. encoding).

Source code in datachain/lib/file.py
1169
1170
1171
1172
1173
1174
1175
1176
def read_text(self, **open_kwargs):
    """Return file contents as text.

    **open_kwargs : Any
        Extra keyword arguments forwarded to ``open()`` (e.g. encoding).
    """
    with self.open(**open_kwargs) as stream:
        return stream.read()

save

save(destination: str, client_config: dict | None = None, *, content_type: str | None = None, content_disposition: str | None = None, cache_control: str | None = None, content_encoding: str | None = None, metadata: dict[str, str] | None = None, write_options: dict[str, Any] | None = None) -> TextFile

Writes its content to destination

Source code in datachain/lib/file.py
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
def save(
    self,
    destination: str,
    client_config: dict | None = None,
    *,
    content_type: str | None = None,
    content_disposition: str | None = None,
    cache_control: str | None = None,
    content_encoding: str | None = None,
    metadata: dict[str, str] | None = None,
    write_options: dict[str, Any] | None = None,
) -> "TextFile":
    """Writes its content to destination"""
    result = super().save(
        destination,
        client_config=client_config,
        content_type=content_type,
        content_disposition=content_disposition,
        cache_control=cache_control,
        content_encoding=content_encoding,
        metadata=metadata,
        write_options=write_options,
    )
    tf = TextFile(**result.model_dump())
    tf._set_stream(self._catalog)
    return tf