<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/ops/io_ops.py#L97-L134) |

Reads the contents of file.

#### View aliases


**Compat aliases for migration**

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

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

<br />

    tf.io.read_file(
        filename, name=None
    )

### Used in the notebooks

| Used in the guide | Used in the tutorials |
|---|---|
| - [tf.data: Build TensorFlow input pipelines](https://www.tensorflow.org/guide/data) | - [Simple audio recognition: Recognizing keywords](https://www.tensorflow.org/tutorials/audio/simple_audio) - [pix2pix: Image-to-image translation with a conditional GAN](https://www.tensorflow.org/tutorials/generative/pix2pix) - [Transfer learning with YAMNet for environmental sound classification](https://www.tensorflow.org/tutorials/audio/transfer_learning_audio) - [Adversarial example using FGSM](https://www.tensorflow.org/tutorials/generative/adversarial_fgsm) - [Neural style transfer](https://www.tensorflow.org/tutorials/generative/style_transfer) |

This operation returns a tensor with the entire contents of the input
filename. It does not do any parsing, it just returns the contents as
they are. Usually, this is the first step in the input pipeline.

#### Example:

    with open("/tmp/file.txt", "w") as f:
      f.write("asdf")

    4
    tf.io.read_file("/tmp/file.txt")
    <tf.Tensor: shape=(), dtype=string, numpy=b'asdf'>

Example of using the op in a function to read an image, decode it and reshape
the tensor containing the pixel data:

    @tf.function
    def load_image(filename):
      raw = tf.io.read_file(filename)
      image = tf.image.decode_png(raw, channels=3)
      # the `print` executes during tracing.
      print("Initial shape: ", image.shape)
      image.set_shape([28, 28, 3])
      print("Final shape: ", image.shape)
      return image

<br />

<br />

<br />

| ## Args ||
|---|---|
| `filename` | string. filename to read from. |
| `name` | string. Optional name for the op. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A tensor of dtype "string", with the file contents. ||

<br />