# tf.io.TFRecordWriter

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 1 version](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/io/TFRecordWriter) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/lib/io/tf_record.py#L192-L246) |

A class to write records to a TFRecords file.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.io.TFRecordWriter`](https://www.tensorflow.org/api_docs/python/tf/io/TFRecordWriter), [`tf.compat.v1.python_io.TFRecordWriter`](https://www.tensorflow.org/api_docs/python/tf/io/TFRecordWriter)

<br />

    tf.io.TFRecordWriter(
        path, options=None
    )

This class implements `__enter__` and `__exit__`, and can be used
in `with` blocks like a normal file.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `path` | The path to the TFRecords file. |
| `options` | (optional) String specifying compression type, `TFRecordCompressionType`, or `TFRecordOptions` object. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `IOError` | If `path` cannot be opened for writing. |
| `ValueError` | If valid compression_type can't be determined from `options`. |

<br />

## Methods

### `close`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/lib/io/tf_record.py#L243-L246)

    close()

Close the file.

### `flush`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/lib/io/tf_record.py#L238-L241)

    flush()

Flush the file.

### `write`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/lib/io/tf_record.py#L229-L236)

    write(
        record
    )

Write a string record to the file.

<br />

<br />

<br />

| Args ||
|---|---|
| `record` | str |

<br />

### `__enter__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/lib/io/tf_record.py#L221-L223)

    __enter__()

Enter a `with` block.

### `__exit__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/lib/io/tf_record.py#L225-L227)

    __exit__(
        unused_type, unused_value, unused_traceback
    )

Exit a `with` block, closing the file.