Nikita Belooussov
Nikita Belooussov

Reputation: 606

Python unable to open a .h5 file

I am trying to open a HDF5 file in order to read it with python, so that I can do more things with it later. There is an error when I run the program to read the file. The program is below:

import h5py    # HDF5 support
import numpy

fileName = "C:/.../file.h5"
f = h5py.File(fileName,  "r")
for item in f.attrs.keys():
    print item + ":", f.attrs[item]
mr = f['/entry/mr_scan/mr']
i00 = f['/entry/mr_scan/I00']
print "%s\t%s\t%s" % ("#", "mr", "I00")
for i in range(len(mr)):
    print "%d\t%g\t%d" % (i, mr[i], i00[i])
f.close()

If I run the program I end up seeing this error:

Traceback (most recent call last):
 File "TestHD5.py", line 8, in <module>
    mr = f['/entry/mr_scan/mr']
 File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (C:\aroot\work\h5py\_objects.c:2587)
 File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (C:\aroot\work\h5py\_objects.c:2546)
 File "C:\programs\Python27\lib\site-packages\h5py\_hl\group.py", line 166, in __getitem__
    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
 File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (C:\aroot\work\h5py\_objects.c:2587)
 File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (C:\aroot\work\h5py\_objects.c:2546)
 File "h5py\h5o.pyx", line 190, in h5py.h5o.open (C:\aroot\work\h5py\h5o.c:3417)
KeyError: 'Unable to open object (Component not found)'

Am I just missing some modules to read the file, or is this something else. It will open the .h5 file if I use an h5 file veiwer program. Thank you

Upvotes: 1

Views: 8490

Answers (2)

Nikita Belooussov
Nikita Belooussov

Reputation: 606

The answer that @NickT posted fixed the orginal problem I had. The problem that is shown in the new version is due to the hd5 folder names in the hd5 file not matching the folder names that the code provided.

Upvotes: 0

Nick T
Nick T

Reputation: 26757

Your string:

path = "C:\Users\312001\m2020\data\20170104_145626\doPoint_20170104_150016\dataset_XMIT    data_20170104_150020.h5"

is full of broken/illegal escapes (thankfully they will be turned into SyntaxErrors, though you are using Python 2), and some that actually do work, so Python thinks path is really equal to: 'C:\\Users\xca001\\m2020\\data\x8170104_145626\\doPoint_20170104_150016\\dataset_XMIT data_20170104_150020.h5' (note those \x##'s).

Your options:

  1. Use a raw string by prefixing the string literal with r
  2. Don't use backslashes for paths. Python will convert forward slashes to backslashes for Windows paths.
  3. Double-backslash.

Upvotes: 3

Related Questions