Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Please don't post images of your code or output. Paste the code & output directly, so it's much easier to work with. – Rushy Panchal Jan 4, 2017 at 23:26 Avoid backslashes in paths like the plague. Python will happily convert forward-slashes (/) to backslashes on Windows for you. – Nick T Jan 4, 2017 at 23:46

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:

  • Use a raw string by prefixing the string literal with r
  • Don't use backslashes for paths. Python will convert forward slashes to backslashes for Windows paths.
  • Double-backslash.
  • Thanks that got rid UnicodeDecodeError and some of the other lines that the terminal was displaying. Ill edit it so that you can see what it shows now. I am still unable to open the file. – Nikita Belooussov Jan 5, 2017 at 0:02

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.