Passing file objects into `h5py.File` is tricky
See original GitHub issue- Operating System Windows 10
- Python version 3.6
- Built with pyenv
- h5py version (2.10 & 3.0.0rc1)
- HDF5 version 1.12.0
Summary of the h5py configuration
---------------------------------
h5py 3.0.0rc1
HDF5 1.12.0
Python 3.6.10 (default, Mar 20 2020, 00:41:24)
[GCC 7.5.0]
sys.platform linux
sys.maxsize 9223372036854775807
numpy 1.17.4
cython (built with) 0.29.21
numpy (built against) 1.12.0
HDF5 (built against) 1.12.0
I thought you could create HDF5 files from file objects but I get “I/O operation on closed file” errors. What’s going wrong here?
>>> with open("test.hdf5", "w") as f:
... h = h5py.File(f, "w")
... h.create_group("ehe")
...
Traceback (most recent call last):
File "h5py/_objects.pyx", line 199, in h5py._objects.ObjectID.__dealloc__
File "h5py/h5fd.pyx", line 169, in h5py.h5fd.H5FD_fileobj_write
ValueError: I/O operation on closed file.
Exception ignored in: 'h5py._objects.ObjectID.__dealloc__'
Traceback (most recent call last):
File "h5py/_objects.pyx", line 199, in h5py._objects.ObjectID.__dealloc__
File "h5py/h5fd.pyx", line 169, in h5py.h5fd.H5FD_fileobj_write
ValueError: I/O operation on closed file.
<HDF5 group "/ehe" (0 members)>
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (10 by maintainers)
Top Results From Across the Web
File Objects — h5py 3.7.0 documentation
HDF5 files work generally like standard Python file objects. ... This is the default if a file-like object is passed to File ....
Read more >Reading an hdf5 file only after it has completely finished ...
It returns the h5py file object if it opens the file. · It immediately returns None if the file doesn't exist. · If...
Read more >How to use HDF5 files in Python
In this article, we will see how you can use h5py to store and retrieve data from files. We will discuss different ways...
Read more >Allow reading files passing file objects #1299 - pytroll/satpy
If for some reason, the filename is still needed, a potential strategy could be to allow the user to explicitly provide a filename,...
Read more >h5py Documentation - Read the Docs
encoding before being passed to the HDF5 C library. Objects ... Like hard links in a UNIX file system, objects in an HDF5...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok, turns out there are no real actionable issues here. There’s 3 things going on:
h5py
v3.0.0 changing the behavior of missing file modes inh5py.File
to an error that I had not seen before since I just switched to 3.0.0In summary, be sure to:
f = open(filename, "wb+")
h5py.File
constructor:h = h5py.File(f, "w")
h5py.File
handle BEFORE you close your file handleThe correct way to pass a file object to
h5py.File
:With
with
:I think there are some improvements that can be made:
mode
” (https://github.com/h5py/h5py/issues/1703)File
object and the underlying handle is already closed, there’s a segfaultUsing file-like objects with h5py was new in version 2.9. The docs do say that. 😉
https://docs.h5py.org/en/stable/high/file.html#python-file-like-objects