# FilePublisher

## class FilePublisher( directory: str | Path | None = None, format: Literal['json', 'jsonl', 'csv', 'avro'] = 'avro', custom_file_name: str | None = None, *, file: IO[Any] | None = None, )[source]

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object "(in Python v3.15)")

Write Measurement/Command data to a file or to an open stream.

Parameters:
:   - **directory**

      Output directory, created if missing. Defaults to `instro-measurements` in the current
      working directory.
    - **format**

      `"jsonl"`, `"csv"`, `"avro"` (default), or `"json"` (deprecated).
    - **custom_file_name**

      Filename without extension; defaults to `measurements-<local-timestamp>`.
    - **file**

      Open stream to write to instead of a file: binary for `"avro"`, text for `"jsonl"` and
      `"csv"`. `"json"` does not support streams. `close()` leaves the stream open.

Raises:
:   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `file` is combined with `directory`, `custom_file_name`, or `"json"`, the format
    is unknown, or `file` is the wrong kind of stream for the format.

Methods

| [`close`](https://dev.nominal.io/instro/ref/library/instro.lib.publishers.files.FilePublisher.md#instro.lib.publishers.files.FilePublisher.close "instro.lib.publishers.files.FilePublisher.close") | Close the file this publisher opened. |
| --- | --- |
| [`open`](https://dev.nominal.io/instro/ref/library/instro.lib.publishers.files.FilePublisher.md#instro.lib.publishers.files.FilePublisher.open "instro.lib.publishers.files.FilePublisher.open") |  |
| [`publish`](https://dev.nominal.io/instro/ref/library/instro.lib.publishers.files.FilePublisher.md#instro.lib.publishers.files.FilePublisher.publish "instro.lib.publishers.files.FilePublisher.publish") | Publish data to file using the appropriate writer. |

### directory: Path | None

### file_path: Path | None

### publish( data: Measurement | Command, **kwargs, ) → None[source]

Publish data to file using the appropriate writer.

### open() → None[source]

### close() → None[source]

Close the file this publisher opened. A stream passed as `file` stays open.
