简体   繁体   中英

How do I keep an h5py group in memory after closing the file?

How do I keep an h5py group in memory after closing the file?

After the following code:

import h5py

feature_file = h5py.File(worm_file_path, 'r')
worm_features = feature_file["worm"]

I can access worm_features as it is an h5py group ( <HDF5 group "/worm" (4 members)> )

But after I run the line:

feature_file.close()

I can no longer access worm_features . It now appears as <Closed HDF5 group> .

Since I need to load the worm_features h5py group for about 20 files, I'd like to close those files before doing processing on the data I've loaded into memory. Is this not possible?

Use .value to pull the variable you want from the dataset.

Example:

import h5py

feature_file = h5py.File(worm_file_path, 'r')
worm_features = feature_file["worm"].value
feature_file.close()
print worm_features

Use [:] to copy the value of dataset to a variable to have access the dataset after closing the hdf5 file.

import h5py

feature_file = h5py.File(worm_file_path, 'r')
worm_features = feature_file["worm"] [:]
feature_file.close()
print (worm_features)

.value does not work for me and raised following error:

AttributeError: 'Dataset' object has no attribute 'value'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM