How to Read a File into a Tensor in TensorFlow with tf.io.read_file
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
tf.io.read_file(<path>)
| Part | What 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
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())

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:
uv run python read_demo.py
tf.Tensor(b'3.14\n', shape=(), dtype=string)
() <dtype: 'string'>
b'3.14\n'
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
| Mistake | What happens | Do this instead |
|---|---|---|
A wrong file name: ".python_version" with an underscore | NotFoundError: {{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 folder | The same NotFoundError, because a relative path starts from the folder you run Python in | Run 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
- tf.io.read_file — the API page.
- String tensors — how TensorFlow stores bytes and text.
