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

&引用;“文件存在”;尝试使用h5py库在python中打开.mat文件时出错,python,matlab,h5py,Python,Matlab,H5py,我尝试使用以下代码访问.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-pa

我尝试使用以下代码访问.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)
回溯(最近一次呼叫最后一次):
文件“/miniconda3/lib/python3.6/site packages/h5py/_hl/files.py”,第190行,在make_fid中
fid=h5f.open(名称,h5f.ACC_RDWR,fapl=fapl)
文件“h5py/_objects.pyx”,第54行,在h5py._objects.with_phil.wrapper中
文件“h5py/_objects.pyx”,第55行,在h5py._objects.with_phil.wrapper中
文件“h5py/h5f.pyx”,第85行,在h5py.h5f.open中
OSError:无法打开文件(未找到文件签名)
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/miniconda3/lib/python3.6/site packages/h5py/_hl/files.py”,第193行,在make_fid中
fid=h5f.open(名称,仅h5f.ACC,fapl=fapl)
文件“h5py/_objects.pyx”,第54行,在h5py._objects.with_phil.wrapper中
文件“h5py/_objects.pyx”,第55行,在h5py._objects.with_phil.wrapper中
文件“h5py/h5f.pyx”,第85行,在h5py.h5f.open中
OSError:无法打开文件(未找到文件签名)
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“test.py”,第4行,在
g=h5py.File('maskH07.mat')
文件“/miniconda3/lib/python3.6/site packages/h5py/_-hl/files.py”,第394行,在__
swmr=swmr)
文件“/miniconda3/lib/python3.6/site packages/h5py/_hl/files.py”,第195行,在make_fid中
fid=h5f.create(名称,h5f.ACC_EXCL,fapl=fapl,fcpl=fcpl)
文件“h5py/_objects.pyx”,第54行,在h5py._objects.with_phil.wrapper中
文件“h5py/_objects.pyx”,第55行,在h5py._objects.with_phil.wrapper中
文件“h5py/h5f.pyx”,第105行,位于h5py.h5f.create中
OSError:无法创建文件(无法打开文件:name='maskH07.mat',errno=17,错误消息='file exists',flags=15,o_flags=a02)

如果您想阅读您可以使用的文件(文件必须存在),将非常感谢您的帮助

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

如果您想要两者,您可以使用(如果文件存在,则截断它,否则创建它)


请参阅和

是否确定该文件位于python工作目录中?使用
print(os.listdir())
print(os.getcwd())
轻松检查。
g = h5py.File('/Path/to/file.mat', 'r')
g = h5py.File('/Path/to/file.mat', 'w')
g = h5py.File('/Path/to/file.mat', 'r+')