Skip to main content

How to Read a File into a Tensor in TensorFlow with tf.io.read_file

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Before TensorFlow can work on a photo or a text file, it has to read the file's bytes. tf.io.read_file does that, and hands back the whole file as one tensor. This guide reads a small text file with it in TensorFlow 2.22.0, then turns the bytes back into text.

Syntax​

Syntax
tf.io.read_file(<path>)
PartWhat it means
<path>The file to read, as text such as "photo.jpg", or a string tensor. A relative path starts from the folder you run Python in

The tf.io.read_file API page: "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."

The Example​

Every uv project has a small file named .python-version, which says which Python the project uses. The script reads it, then prints the tensor, its shape and type, its bytes, and the text.

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Write the Script​

read_demo.py
import tensorflow as tf

data = tf.io.read_file(".python-version")

print(data)
print(data.shape, data.dtype)
print(data.numpy())
print(data.numpy().decode())

The read_demo.py script in a macOS-style code window: data = tf.io.read_file(".python-version"), then prints of data, its shape and dtype, data.numpy(), and data.numpy().decode().

tf.io.read_file reads the file's bytes into data. .numpy() gets those bytes out of the tensor, and .decode() turns bytes into Python text.

Step 2: Run It​

Run it from the project folder, where .python-version is:

Terminal
uv run python read_demo.py
Output
tf.Tensor(b'3.14\n', shape=(), dtype=string)
() <dtype: 'string'>
b'3.14\n'
3.14

The terminal after uv run python read_demo.py: a string tensor holding b'3.14\n', its shape () and dtype string, the bytes b'3.14\n', and the text 3.14.

The whole file became one value: a tensor with the shape () and the type string. The b in front of '3.14\n' means bytes, and \n is the line break at the end of the file. After .decode(), the text printed as 3.14. The command works unchanged in PowerShell.

The first run in a new environment is slow, while TensorFlow loads for the first time.

Common Mistakes​

MistakeWhat happensDo this instead
A wrong file name: ".python_version" with an underscoreNotFoundError: {{function_node __wrapped__ReadFile_device_/job:localhost/replica:0/task:0/device:CPU:0}} .python_version; No such file or directory [Op:ReadFile]Check the name: the error message shows the path it tried
Running the script from another folderThe same NotFoundError, because a relative path starts from the folder you run Python inRun it from the project folder, or build the path from the script's own location, as Fixed: NotFoundError: No such file or directory [Op:ReadFile] shows

FAQ​

Does tf.io.read_file understand images?​

No, it only reads bytes. For a JPEG photo, pass the bytes to tf.io.decode_jpeg, which turns them into a tensor of pixels.

Why is the tensor's type string for a file that isn't text?​

In TensorFlow, string means raw bytes of any kind. The tensor guide says: "The tf.string dtype is used for all raw bytes data in TensorFlow."

Conclusion​

tf.io.read_file read .python-version into a single string tensor holding the file's bytes, b'3.14\n'. .numpy().decode() turned them into text. A wrong name or the wrong working folder ends in a NotFoundError that shows the path it tried.

Additional Resources​