1
$\begingroup$
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:

h5py.File.close(hf)

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
    obj.close()

while

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

, the file is not closed yet.

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

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.
|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thank you for your response but it doesn't also work. $\endgroup$ – Fatemeh Asgarinejad 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$ – Media 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$ – Fatemeh Asgarinejad 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.