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$
2
  • $\begingroup$ I think the question is best suited for StackOverflow $\endgroup$ Commented May 26, 2019 at 6:58
  • $\begingroup$ as you may have noticed, the outcome is indicating that it's a closed object. $\endgroup$ Commented May 27, 2019 at 2:12

1 Answer 1

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.
$\endgroup$
3
  • $\begingroup$ Thank you for your response but it doesn't also work. $\endgroup$ Commented May 27, 2019 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$ Commented May 27, 2019 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$ Commented May 27, 2019 at 7:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.