Skip to main content

Fixed: NotFoundError: No such file or directory [Op:ReadFile]

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

tf.io.read_file("data/notes.txt") stopped with NotFoundError: … data/notes.txt; No such file or directory [Op:ReadFile], even though the file was there. The path was relative, and I had run the script from its own folder, so TensorFlow looked for data in the wrong place. Running it from the project folder fixed it, and so did building the path from the script's own location.

What I Was Doing​

My project keeps its data in a data folder and its scripts in a scripts folder:

fx-readfile-notfound/
fx-readfile-notfound/
├── data/
│ └── notes.txt
├── scripts/
│ └── read_notes.py
└── pyproject.toml

The project also has the other files uv init makes. data/notes.txt holds one line, Buy milk, and the script reads it:

scripts/read_notes.py
import tensorflow as tf

notes = tf.io.read_file("data/notes.txt")
print(notes)

I went into the scripts folder and ran the script there, with TensorFlow 2.22.0:

Terminal
cd scripts
uv run python read_notes.py

What I Expected​

A string tensor with the file's contents: b'Buy milk\n'.

What I Got​

Output
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
W0000 00:00:1796445005.792971 182419 op_kernel.cc:1934] OP_REQUIRES failed at whole_file_read_ops.cc:120 : NOT_FOUND: data/notes.txt; No such file or directory
=== Source Location Trace: ===
external/xla/xla/tsl/platform/errors.cc:181
tensorflow/core/kernels/whole_file_read_ops.cc:44

W0000 00:00:1796445005.793067 182419 local_rendezvous.cc:461] Local rendezvous is aborting with status: NOT_FOUND: data/notes.txt; No such file or directory
=== Source Location Trace: ===
external/xla/xla/tsl/platform/errors.cc:181
tensorflow/core/kernels/whole_file_read_ops.cc:44
tensorflow/core/common_runtime/single_threaded_executor.cc:449
tensorflow/core/common_runtime/function.cc:1418

Traceback (most recent call last):
File "/Users/you/source/fx-readfile-notfound/scripts/read_notes.py", line 3, in <module>
notes = tf.io.read_file("data/notes.txt")
File "/Users/you/source/fx-readfile-notfound/.venv/lib/python3.14/site-packages/tensorflow/python/ops/io_ops.py", line 134, in read_file
return gen_io_ops.read_file(filename, name)
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
File "/Users/you/source/fx-readfile-notfound/.venv/lib/python3.14/site-packages/tensorflow/python/ops/gen_io_ops.py", line 583, in read_file
return read_file_eager_fallback(
filename, name=name, ctx=_ctx)
File "/Users/you/source/fx-readfile-notfound/.venv/lib/python3.14/site-packages/tensorflow/python/ops/gen_io_ops.py", line 606, in read_file_eager_fallback
_result = _execute.execute(b"ReadFile", 1, inputs=_inputs_flat,
attrs=_attrs, ctx=ctx, name=name)
File "/Users/you/source/fx-readfile-notfound/.venv/lib/python3.14/site-packages/tensorflow/python/eager/execute.py", line 53, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.NotFoundError: {{function_node __wrapped__ReadFile_device_/job:localhost/replica:0/task:0/device:CPU:0}} data/notes.txt; No such file or directory [Op:ReadFile]
W0000 00:00:1796445005.962363 182419 local_rendezvous.cc:461] Local rendezvous is aborting with status: ABORTED: Shutdown
=== Source Location Trace: ===
./tensorflow/core/common_runtime/eager/rendezvous_cache.h:42

The terminal after cd scripts and uv run python read_notes.py: TensorFlow's W0000 log lines saying NOT_FOUND: data/notes.txt; No such file or directory, then a traceback ending in NotFoundError: data/notes.txt; No such file or directory, for the ReadFile operation.

The lines starting with W0000 are TensorFlow's own log: the ReadFile operation failed with NOT_FOUND. The traceback after them ends in the error itself, and the last W0000 line is TensorFlow shutting down after it.

What I Checked​

1. Which path TensorFlow looked for​

The message shows the path exactly as the script gave it: data/notes.txt. It doesn't start with /, so it's a relative path: it says where the file is starting from some folder, not from the top of the disk.

2. Which folder a relative path starts from​

From the folder the command runs in, called the current working directory. Python's open() documentation describes a file name as "absolute or relative to the current working directory". I ran the script inside scripts, so data/notes.txt meant scripts/data/notes.txt, which doesn't exist.

3. Whether uv run changes the folder​

It doesn't: it runs the command in the folder you're in. uv run --help lists a separate option for running somewhere else: "--directory <DIRECTORY>: Change to the given directory prior to running the command".

Why It Happens​

A relative path is looked up from the folder you run the command in, not from the folder the script is saved in. The file was at data/notes.txt from the project folder, and I ran the script one folder deeper.

The Fix​

Run the script from the project folder, where the path starts in the right place:

Terminal
cd ..
uv run python scripts/read_notes.py

cd .. goes up one folder, back to the project folder. From there the script is at scripts/read_notes.py, and data/notes.txt is where the path says.

Another way: build the path from the script's own location​

To make the script work from any folder, build the path from where the script is saved instead of using a relative one:

scripts/read_notes_anywhere.py
from pathlib import Path

import tensorflow as tf

project_dir = Path(__file__).resolve().parent.parent
notes = tf.io.read_file(str(project_dir / "data" / "notes.txt"))
print(notes)

__file__ is the script's own file name. Python's data model documentation says it "indicates the pathname of the file from which the module was loaded". .resolve() makes the path absolute, and each .parent goes up one folder: the first to scripts, the second to the project folder. str() turns the result into text, which tf.io.read_file needs. Run from inside scripts:

Terminal
uv run python read_notes_anywhere.py
Output
tf.Tensor(b'Buy milk\n', shape=(), dtype=string)

The terminal after uv run python read_notes_anywhere.py, run inside the scripts folder: tf.Tensor(b'Buy milk\n', shape=(), dtype=string).

The script read the file from inside scripts, and it would from any other folder too.

After the Fix​

From the project folder, the original script read the file:

Terminal
uv run python scripts/read_notes.py
Output
tf.Tensor(b'Buy milk\n', shape=(), dtype=string)

The terminal after uv run python scripts/read_notes.py from the project folder: tf.Tensor(b'Buy milk\n', shape=(), dtype=string).

The file's contents came back: b'Buy milk\n'. The commands on this page work unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Changing the path to "../data/notes.txt"It works from scripts, and fails from the project folder: ../data/notes.txt; No such file or directory [Op:ReadFile]Pick one folder to run from, or build the path from __file__
Passing the Path itself: tf.io.read_file(project_dir / "data" / "notes.txt")ValueError: Attempt to convert a value (PosixPath(…)) with an unsupported type … to a Tensor.Wrap it in str(). Fixed: ValueError: Attempt to convert a value (PosixPath(…)) with an unsupported type to a Tensor explains why

Troubleshooting​

SymptomLikely causeFix
data/notes.txt; No such file or directory [Op:ReadFile] from a subfolderThe relative path starts from the folder you ran the command inRun it from the project folder, or build the path from __file__
The same error from the project folderThe file isn't where the path saysList the folder: ls data printed notes.txt. Check the name's spelling and case
Attempt to convert a value (PosixPath(…))A Path passed to tf.io.read_filestr(path)

FAQ​

How do I see which folder my script runs in?​

Print it with os.getcwd(), after import os. Run inside scripts, it printed the full path of the scripts folder.

Can uv run the script from the project folder for me?​

Yes, with --directory. Inside scripts, uv run --directory .. python scripts/read_notes.py read the file: uv went up to the project folder first, then ran the script from there.

Conclusion​

NotFoundError: … data/notes.txt; No such file or directory [Op:ReadFile] meant the relative path data/notes.txt started from the scripts folder, where I'd run the script. Running it from the project folder fixed it, and building the path from Path(__file__) made it work from any folder.

Additional Resources​