“文件存在”在尝试使用h5py库在python中打开.mat文件时出现错误

问题描述 投票:0回答:1

我尝试使用以下代码访问.mat文件中的部分数据(称为“掩码”):

import h5py
import numpy as np

g = h5py.File('/Path/to/file.mat')
x = g["mask"]
print(np.array(x))

对于另一个.mat文件,这似乎完全可以正常工作,但是对于这个文件,我总是收到以下错误消息:

Traceback (most recent call last):
File "/miniconda3/lib/python3.6/site-packages/h5py/_hl/files.py", line 190, in make_fid
fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py/h5f.pyx", line 85, in h5py.h5f.open
OSError: Unable to open file (file signature not found)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/miniconda3/lib/python3.6/site-packages/h5py/_hl/files.py", line 193, in make_fid
fid = h5f.open(name, h5f.ACC_RDONLY, fapl=fapl)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py/h5f.pyx", line 85, in h5py.h5f.open
OSError: Unable to open file (file signature not found)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "test.py", line 4, in <module>
g = h5py.File('maskH07.mat')
File "/miniconda3/lib/python3.6/site-packages/h5py/_hl/files.py", line 394, in __init__
swmr=swmr)
File "/miniconda3/lib/python3.6/site-packages/h5py/_hl/files.py", line 195, in make_fid
fid = h5f.create(name, h5f.ACC_EXCL, fapl=fapl, fcpl=fcpl)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py/h5f.pyx", line 105, in h5py.h5f.create
OSError: Unable to create file (unable to open file: name = 'maskH07.mat', errno = 17, error message = 'File exists', flags = 15, o_flags = a02)

非常感谢您的帮助

python matlab h5py
1个回答
0
投票

如果您想阅读可以使用的文件(文件必须存在)

g = h5py.File('/Path/to/file.mat', 'r')

如果要写入文件,可以使用(文件必须存在)

g = h5py.File('/Path/to/file.mat', 'w')

如果您希望两者都可以使用(如果文件存在,则将其截断,否则创建)

g = h5py.File('/Path/to/file.mat', 'r+')

请参见documentationother post

© www.soinside.com 2019 - 2024. All rights reserved.