h5py.File(path)无法识别文件夹路径

时间:2019-08-12 22:38:56

标签: python path jupyter-notebook load h5py

我在项目文件夹中称为“项目”。我有两个神经网络h5文件,一个在“ project / my_folder / my_model_1.h5”中,我还将其复制到文件夹“ project / my_model_2.h5”中。因此,我打开了在“项目”文件夹下工作的Jupyter Notebook。

import h5py
f = h5py.File("my_model_2.h5") # has NO Issue

但是

f = h5py.File("my_folder/my_model_1.h5") # OSError

上面写着OSError: Unable to open file (unable to open file: name = 'my_folder/my_model_1.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

有趣的是,只有在Mac上执行相同的操作时,我才会遇到此问题,但在Linux机器上没有遇到任何问题。

如果您知道如何解决此问题,请告诉我。预先谢谢你。

2 个答案:

答案 0 :(得分:0)

因此,当我只是从Mac文件夹系统复制并粘贴文件路径时,似乎偶然地复制了一些隐藏的无效字符。看一下屏幕上的代码。 enter image description here

第92行是我直接从Mac文件夹复制并粘贴的路径名。

第93行是我逐字键入的路径,因此没有错误,并且.h5文件已正确加载。有人在此链接上发现了类似的问题:Invalid character in identifier

我只是将错误代码复制到Pycharm,不受欢迎的字符就被删除了。

enter image description here

因此,对于Mac用户,解决方案仅是简单地从文件夹系统中复制文本,如果有明显的怪异,请尝试在文本编辑器中键入每个字母。

答案 1 :(得分:0)

使用在Windows中运行的操作系统指定绝对路径

file_name = os.path.dirname(__file__) +'\\my_folder\\my_model_1.h5'
f = h5py.File(file_name)

不要忘记导入操作系统