简体   繁体   English

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

[英]“File exists” error when trying to open .mat file in python with h5py library

I try to access part of the data (which is called "mask") in a.mat file using the following code:我尝试使用以下代码访问 a.mat 文件中的部分数据(称为“掩码”):

import h5py
import numpy as np

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

This seems to work pefectly fine for another.mat file but somehow for this one i keep getting the following error message:这对于 another.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)

Would greatly appreciate any help非常感谢任何帮助

if you want to read the file you can use (File must exist)如果要读取可以使用的文件(文件必须存在)

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

if you want write to the file you can use (File must exist)如果要写入可以使用的文件(文件必须存在)

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

if you want both you can use (If file exist truncate it else create it)如果你想要两者都可以使用(如果文件存在截断它,否则创建它)

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

See documentation and other post查看文档其他帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM