question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 3 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
Helvegcommented, Oct 12, 2020

Ok, turns out there are no real actionable issues here. There’s 3 things going on:

  1. Me not opening file handles in binary mode
  2. The context managers (with statements) closing the underlying file handle
  3. h5py v3.0.0 changing the behavior of missing file modes in h5py.File to an error that I had not seen before since I just switched to 3.0.0

  1. This is mentioned in the docs

The file-like object must be open for binary I/O

  1. Also mentioned in the docs

Accessing the File instance after the underlying file object has been closed will result in undefined behaviour.

  1. Very unfortunate as this broke the documentation example (#1702) strengthening my belief there was a large collection of errors surrounding file objects

In summary, be sure to:

  • Open your file handle with read/write permissions in binary mode: f = open(filename, "wb+")
  • Explicitly specify a file mode argument to the h5py.File constructor: h = h5py.File(f, "w")
  • Close your h5py.File handle BEFORE you close your file handle

The correct way to pass a file object to h5py.File:

f = open("test.h5", "wb+")
h = h5py.File(f, "w")
# ...
h.close()
f.close()

With with:

with open("test.h5", "wb+") as f:
  with h5py.File(f, "w") as h:
    # ...

I think there are some improvements that can be made:

  • Instead of allowing undefined behavior, throw an exception when a closed file handle is encountered
  • Instead of the long and cryptic error message, use the default Python “TypeError: File() is missing required argument mode” (https://github.com/h5py/h5py/issues/1703)
  • When closing the File object and the underlying handle is already closed, there’s a segfault
0reactions
takluyvercommented, Dec 12, 2020

Using 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found