How to close HDF5 using the low level Python API?

I was able to change the HDF5 file cache settings by combining the high and low level Python h5py APIs, as defined in the following stack overflow question: How do I set cache parameters when using the high level h5py interface?

I get an error that the h5 file is still open when I try to rename the file. The Python statement with the instruction with contextlib does not seem to close the file after recording is completed in HDF5 format, and the file is cleared. How can I make sure the file is closed using a low-level or high-level API? Could you give an example?

import h5py import contextlib import os filename = 'foo_2.h5' propfaid = h5py.h5p.create(h5py.h5p.FILE_ACCESS) settings = list(propfaid.get_cache()) settings[2] *= 5 propfaid.set_cache(*settings) with h5py.File(filename, 'w') as hf: print 'file created' with contextlib.closing(h5py.h5f.open(filename, fapl=propfaid)) as fid: f = h5py.File(fid) f.flush() # f.close() Seems to be working only in Python 3.4.3 but not in 2.7.7 #and the "with contextlib.closing(...) does not work on either version f.close() os.rename(filename, 'foo_2.h5') 

Additional Information:
OS: Windows
Python: 2.7.7
Anaconda Distribution: 2.0.1
H5py Version: 2.3.0

+1
source share
1 answer

I think you are looking for .close ()

 f.close() 

Although I look closer, I'm not sure why contextlib.closing (...) is not working.

I edited the line involving contextlib:

 with contextlib.closing(h5py.File(filename, fapl=propfaid)) as fid: 

and deleted

 f = h5py.File(fid) 

Subsequently, I could verify that the feed was closed and that the rename file was working as expected.

 print fid <Closed HDF5 file> os.rename(filename, 'foo2.hdf5') 

No errors and directory check shows new name

0
source

All Articles