import h5py #added
hf = h5py.File('../images.h5', 'w') #added
hf.close() #added

h5_file = tables.open_file("images.h5", mode="w")

I also tried:


the error that pops up in both cases is:

ValueError: The file 'restricted_images.h5' is already opened.  Please close it before reopening in write mode.

I've also tried:

if isinstance(obj, h5py.File):   # Just HDF5 files


In[]: hf
Out[]: <Closed HDF5 file> 

, the file is not closed yet.

  • $\begingroup$ I think the question is best suited for StackOverflow $\endgroup$ May 26 '19 at 6:58
  • $\begingroup$ as you may have noticed, the outcome is indicating that it's a closed object. $\endgroup$ May 27 '19 at 2:12

You may want to use something like the following snippet:

with h5py.File("some_path.h5") as f:
   f["data1"] = some_data

import h5py #added
with h5py.File('../images.h5', 'w') as f:    
    h5_file = tables.open_file("images.h5", mode="w")
    # do what ever you want to do, it will be closed by itself.
  • $\begingroup$ Thank you for your response but it doesn't also work. $\endgroup$ May 27 '19 at 2:36
  • $\begingroup$ You meant it did not close the file? Consider that outside of the with the file is going to be closed. In your code snippet, it is also denoting that it's been closed already. $\endgroup$ May 27 '19 at 2:40
  • 1
    $\begingroup$ Based on the error, the problem was with opening an already opened file. Actually, I shouldn't create the images.h5 file myself. Thank you so much for your time. This with thing is really useful by the way. $\endgroup$ May 27 '19 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.