tf.io.read_file
Stay organized with collections
Save and categorize content based on your preferences.
Reads the contents of file.
tf.io.read_file(
filename, name=None
)
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
Args |
filename
|
string. filename to read from.
|
name
|
string. Optional name for the op.
|
Returns |
A tensor of dtype "string", with the file contents.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[],[],null,["# tf.io.read_file\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/ops/io_ops.py#L97-L134) |\n\nReads the contents of file.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`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)\n\n\u003cbr /\u003e\n\n tf.io.read_file(\n filename, name=None\n )\n\nThis operation returns a tensor with the entire contents of the input\nfilename. It does not do any parsing, it just returns the contents as\nthey are. Usually, this is the first step in the input pipeline.\n\n#### Example:\n\n with open(\"/tmp/file.txt\", \"w\") as f:\n f.write(\"asdf\")\n\n 4\n tf.io.read_file(\"/tmp/file.txt\")\n \u003ctf.Tensor: shape=(), dtype=string, numpy=b'asdf'\u003e\n\nExample of using the op in a function to read an image, decode it and reshape\nthe tensor containing the pixel data: \n\n @tf.function\n def load_image(filename):\n raw = tf.io.read_file(filename)\n image = tf.image.decode_png(raw, channels=3)\n # the `print` executes during tracing.\n print(\"Initial shape: \", image.shape)\n image.set_shape([28, 28, 3])\n print(\"Final shape: \", image.shape)\n return image\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|-----------------------------------|\n| `filename` | string. filename to read from. |\n| `name` | string. Optional name for the op. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A tensor of dtype \"string\", with the file contents. ||\n\n\u003cbr /\u003e"]]