tensorflow: Initial read on nonexistent tf.gfile.GFile in w+ mode crashes
System information
- Have I written custom code: Yes
- OS Platform and Distribution: Linux Ubuntu 18.04
- TensorFlow installed from: binary
- TensorFlow version: v1.14.0-rc1-22-gaf24dc9 1.14.0
- Python version: 3.7
Describe the current behavior
Python raises tensorflow.python.framework.errors_impl.NotFoundError when doing a first read (no writes before it) on a nonexistent tf.gfile.GFile in w+ mode.
Describe the expected behavior
Read on an empty w+ file should return an empty string.
One problem with the current behaviour is that numpy.savez() crashes when writing to a GFile.
Code to reproduce the issue
import tensorflow as tf
with tf.io.gfile.GFile('test.txt', 'w+') as f:
f.read()
Other info / logs
Traceback (most recent call last):
File "test_gfile.py", line 5, in <module>
f.read()
File "/VENV/lib/python3.7/site-packages/tensorflow/python/lib/io/file_io.py", line 122, in read
self._preread_check()
File "/VENV/lib/python3.7/site-packages/tensorflow/python/lib/io/file_io.py", line 84, in _preread_check
compat.as_bytes(self.__name), 1024 * 512)
tensorflow.python.framework.errors_impl.NotFoundError: test.txt; No such file or directory
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 15 (5 by maintainers)
Commits related to this issue
- Fix save memory store using GFile. Writing to a buffer to avoid read error in np.savez when using GFile. See: https://github.com/tensorflow/tensorflow/issues/32090 — committed to tensorflow/similarity by owenvallis 2 years ago
- Fix save memory store using GFile. Writing to a buffer to avoid read error in np.savez when using GFile. See: https://github.com/tensorflow/tensorflow/issues/32090 — committed to abeltheo/similarity by owenvallis 2 years ago
I was able to replicate the issue in tf-nightly 2.12.0-dev20221215. Please find the gist for reference. Thank you.
a similar approach that works for me.
from @eirism in https://github.com/tensorflow/tensorflow/issues/32090#issuecomment-526563509:
could you give an example of such a workaround? I tried the following unsuccessful:
Still getting
f.seek()also crashes in the same way asf.read().Related to the fact that GFile does not truncate files the same way as Python, reading an existing file opened with
w+will return the text in the file instead of"".Example:
test.txtis not in your current working directory. Please, make suretest.txtandyour python-file/jupyter-notebookare in same directory.Thanks!