Databases Reference
In-Depth Information
DATASPACE SCALAR
DATA {
(0): 42
}
}
}
}
Your First HDF5 File
Before we get to groups and datasets, let's start by exploring some of the capabilities of
the File object, which will be your entry point into the world of HDF5.
Here's the simplest possible program that uses HDF5:
>>> f = h5py . File ( "name.hdf5" )
>>> f . close ()
The File object is your starting point; it has methods that let you create new datasets
or groups in the file, as well as more pedestrian properties such as .filename and .mode .
Speaking of .mode , HDF5 files support the same kind of read/write modes as regular
Python files:
>>> f = h5py . File ( "name.hdf5" , "w" ) # New file overwriting any existing file
>>> f = h5py . File ( "name.hdf5" , "r" ) # Open read-only (must exist)
>>> f = h5py . File ( "name.hdf5" , "r+" ) # Open read-write (must exist)
>>> f = h5py . File ( "name.hdf5" , "a" ) # Open read-write (create if doesn't exist)
There's one additional HDF5-specific mode, which can save your bacon should you
accidentally try to overwrite an existing file:
>>> f = h5py . File ( "name.hdf5" , "w-" )
This will create a new file, but fail if a file of the same name already exists. For example,
if you're performing a long-running calculation and don't want to risk overwriting your
output file should the script be run twice, you could open the file in w- mode:
>>> f = h5py . File ( "important_file.hdf5" , "w-" )
>>> f . close ()
>>> f = h5py . File ( "important_file.hdf5" , "w-" )
IOError: unable to create file (File accessability: Unable to open file)
By the way, you're free to use Unicode filenames! Just supply a normal Unicode string
and it will transparently work, assuming the underlying operating system supports the
UTF-8 encoding:
>>> name = u"name_eta_ \u03b7 "
>>> f = h5py . File ( name )
>>> print f . filename
name_eta_η
Search WWH ::




Custom Search