Skip to content

Instantly share code, notes, and snippets.

@dfsp-spirit
Created August 30, 2022 11:46
Show Gist options
  • Save dfsp-spirit/45a687afb52a74bb5b2436bc8e842fda to your computer and use it in GitHub Desktop.
Save dfsp-spirit/45a687afb52a74bb5b2436bc8e842fda to your computer and use it in GitHub Desktop.
Copy hdf5 file created with python using shutil
#!/usr/bin/env python
import h5py
import shutil
import numpy as np
import os
filename = "test.h5"
copy_filename = 'test_cpy.h5'
def cleanup():
"""Clean up from previous runs if needed."""
if os.path.exists(filename):
os.remove(filename)
if os.path.exists(copy_filename):
os.remove(copy_filename)
cleanup()
# start
h5f = h5py.File(filename, mode='w')
dset1 = h5f.create_dataset('dset1', (3,3), dtype=float)
dset2 = h5f.create_dataset('dset2', (4,4,4), dtype=float)
dset1[()] = np.zeros((3, 3), dtype=np.float64)
dset2[()] = np.zeros((4, 4, 4), dtype=np.float64)
print(f"File {h5f} has dataset entries: {h5f.keys()}")
#myfile.close()
shutil.copyfile(filename, copy_filename)
copy_h5f = h5py.File(filename, mode='r')
print(f"Copied File {copy_filename} ({copy_h5f}) has dataset entries: {copy_h5f.keys()}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment