尝试打开 h5 文件时出现 filenotfound 错误

Getting filenotfound error when trying to open a h5 file

我有一个包含“catvnoncat”数据集的 h5 文件。当我尝试 运行 以下代码时,出现错误,我将其包含在底部。我尝试从三个不同的来源获取数据集以排除文件损坏的可能性。

我想知道是什么导致了这个问题?

# My Code
import h5py
train_dataset = h5py.File('train_catvnoncat.h5', "r")
# The Error
Traceback (most recent call last):
  File "f:\Downloads\Compressed\coursera-deep-learning-specialization-master_2\coursera-deep-learning-specialization-master\C1 - Neural Networks and Deep Learning\Week 2\Logistic Regression as a Neural Network\lr_utils.py", line 4, in <module>
    train_dataset = h5py.File('train_catvnoncat.h5', "r")
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\h5py\_hl\files.py", line 507, in __init__
    fid = make_fid(name, mode, userblock_size, fapl, fcpl, swmr=swmr)
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\h5py\_hl\files.py", line 220, in make_fid
    fid = h5f.open(name, flags, 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 106, in h5py.h5f.open
FileNotFoundError: [Errno 2] Unable to open file (unable to open file: name = 'train_catvnoncat.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

您的代码正在当前目录中查找,该目录不是文件所在的目录。

根据错误消息,您似乎在 windows。文件 'train_catvnoncat.h5' 是否在您的“下载”文件夹中?在您的系统上找到该文件并复制完整路径。然后你可以更新这个:

train_dataset = h5py.File('train_catvnoncat.h5', "r")

到包含完整路径的内容。类似这样:

train_dataset = h5py.File('C:/Users/Moshen/Downloads/train_catvnoncat.h5', "r")

注意斜线并确保将文件路径更新为实际值。