Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
955 views
in Technique[技术] by (71.8m points)

python - Error opening file in H5PY (File signature not found)

I've been using the following bit of code to open some HDF5 files, produced in MATLAB, in python using H5PY:

import h5py as h5
data='dataset.mat'
f=h5.File(data, 'r')

However I'm getting the following error:

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

I've checked that the files that I'm trying to open are version 7.3 MAT-files and are HDF5 format. In fact I've used H5PY to open the same files successfully before. I've confirmed that the files exist and are accessible so I'm not really sure where the error is coming from. Any advice would be greatly appreciated, thanks in advance : )

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Usually the message File signature not found indicates either:

1. Your file is corrupted.

... is what I think is most likely. You said you've opened the files before. Maybe you forgot closing your file-handle which can corrupt the file. Try checking the file with the HDF5 utility h5debug (available on command line if you've installed the hdf5 lib on your OS, check with dpkg -s libhdf5-dev on Linux).

2. The file is not in HDF5 format.

This is a known cause for your error message. But since you said you made sure, that this is the case and you've opened the files before, I'm giving this just for reference for others that may stumble here:

Since December 2015 (as of version 7.3), Matlab files use the HDF5 based format in their MAT-File Level 5 Containers (more doc). Earlier version MAT-files (v4 (Level 1.0), v6 and v7 to 7.2) are supported by and can be read with the scipy library:

import scipy.io
f = scipy.io.loadmat('dataset.mat')

Otherwise you may try other methods and see whether the error persists:

PyTables is an alternative to h5py and be found here.

import tables
file = tables.open_file('test.mat')

Install using

pip install tables

Python MATLAB Engine is an alternative to read MAT files, if you have matlab installed. Documentation is found here: MATLAB Engine API for Python.

import matlab.engine
mat = matlab.engine.start_matlab()
f = mat.load("dataset.mat", nargout=1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...