H5py: OSError: Unable to open file (File signature not found)

Created on 23 Sep 2016  路  8Comments  路  Source: h5py/h5py

I think this is the same bug as #142.

I am trying to load a MATLAB generated data file into Jupyter using h5py. The file definitely exists. Is this also an older HDF4 format? How can I tell?

import numpy as np, h5py 
import os.path

filename1 = r"input\train_1\1_1_0.mat"

if os.path.isfile(filename1) and os.access(filename1, os.R_OK):
    f = h5py.File(filename1,'r') 
else:
    print("Either file is missing or is not readable")

And the error:

OSError                                   Traceback (most recent call last)
<ipython-input-21-dc964679d992> in <module>()
      5 
      6 if os.path.isfile(filename1) and os.access(filename1, os.R_OK):
----> 7     f = h5py.File(filename1,'r')
      8 else:
      9     print("Either file is missing or is not readable")

C:\Users\reina\Anaconda3\lib\site-packages\h5py\_hl\files.py in __init__(self, name, mode, driver, libver, userblock_size, swmr, **kwds)
    270 
    271                 fapl = make_fapl(driver, libver, **kwds)
--> 272                 fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
    273 
    274                 if swmr_support:

C:\Users\reina\Anaconda3\lib\site-packages\h5py\_hl\files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
     90         if swmr and swmr_support:
     91             flags |= h5f.ACC_SWMR_READ
---> 92         fid = h5f.open(name, flags, fapl=fapl)
     93     elif mode == 'r+':
     94         fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py\_objects.pyx in h5py._objects.with_phil.wrapper (C:\aroot\work\h5py\_objects.c:2579)()

h5py\_objects.pyx in h5py._objects.with_phil.wrapper (C:\aroot\work\h5py\_objects.c:2538)()

h5py\h5f.pyx in h5py.h5f.open (C:\aroot\work\h5py\h5f.c:1813)()

OSError: Unable to open file (File signature not found)

Thanks.

Most helpful comment

I was facing the same issue with my .h5 file. And the problem was that I was not downloading the .h5 file correctly.

I was doing filename.h5->right_click->save link as, which was not downloading the file correctly(or may be the file was getting corrupted). Instead of doing that I downloaded the file as : selected the checkbox with filename.h5 and clicked on download and after that my code worked.

May be this help the one's who are doing the same mistake.

All 8 comments

@mas-dse-greina the message File signature not found suggests the file is either corrupted or not in the HDF5 format. Be aware that all operations are handled by the original HDF5 library, of which h5py is only a wrapper. Try checking the file with HDF5 utilities like h5debug.

I am not sure what exactly is causing the error but a common mistake that people do in Jupyter notebook is they forget to close the h5 before trying to access it again.
So try f.close() and try opening it again.

I was facing the same issue with my .h5 file. And the problem was that I was not downloading the .h5 file correctly.

I was doing filename.h5->right_click->save link as, which was not downloading the file correctly(or may be the file was getting corrupted). Instead of doing that I downloaded the file as : selected the checkbox with filename.h5 and clicked on download and after that my code worked.

May be this help the one's who are doing the same mistake.

For reference: for anyone else getting this issue with a .mat file, it is likely because the file was generated with an older version of MATLAB before it was switched over to using HDF5. See https://stackoverflow.com/questions/4950630/matlab-differences-between-mat-versions

The solution for these older mat files is to read it with something like scipy.io.loadmat: https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.io.loadmat.html

I had this error when I downloaded a pre-trained network model. What the problem ended up being is that the file containing the model had a .hdf5.part extension, and it sufficed to simply switch it to a .hdf5 (without the .part). I hope that helps.

I was facing the same issue with my .h5 file. And the problem was that I was not downloading the .h5 file correctly.

I was doing filename.h5->right_click->save link as, which was not downloading the file correctly(or may be the file was getting corrupted). Instead of doing that I downloaded the file as : selected the checkbox with filename.h5 and clicked on download and after that my code worked.

May be this help the one's who are doing the same mistake.

worked for me.
Thanks

I got this error when the hdf5 was empty - had 0 size.

Folks right-clicking on the Workspace and saving the file as a .mat file might find this error when trying to open it using h5py even in Matlab 2021a. This is because the default method of saving .mat files is using Version 7 or later. In MATLAB Preferences > General > MAT-Files > change it to 7.3

Was this page helpful?
0 / 5 - 0 ratings