3

I am in my project folder call "project". I have two neural network h5 file, one in "project/my_folder/my_model_1.h5", I also copy it to folder "project/my_model_2.h5". So I open my Jupyter Notebook which is working at "project" folder.

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

but

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

It says 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)

Interestingly, I only have this issue when I do the same thing on my Mac, but I don't encounter any issue in Linux machine.

Please let me know if you know how to fix this. Thank you in advance.

Jason
  • 3,166
  • 3
  • 20
  • 37
  • Have you tried `h5py.File("./my_folder/my_model_1.h5")`? The added`'./'` is the current folder/directory. – kcw78 Aug 12 '19 at 22:44
  • @kcw78 I just tried, and it still doesn't work – Jason Aug 13 '19 at 04:11
  • What happens if you check with `assert os.path.isfile("my_folder/my_model_1.h5")`? – titusjan Aug 13 '19 at 06:28
  • @titusjan AssertionError: No other info following the the AssertionError. And I specifically check the current working directory using `os.getcwd()`, I'm sure the folder and file are there – Jason Aug 13 '19 at 14:32

2 Answers2

1

So it looks like some hidden invalid character incidentally got copied when I simply copy and paste the file path from Mac folder system. Take a look at the code in the screen. enter image description here

The Line 92 is the path name I directly copy and paste from Mac folder.

The Line 93 is the path I literally type with every single letter, then there is no error and .h5 file is loaded properly. It's a kinda of similar issue that has been spotted by someone at this link: Invalid character in identifier

I simply copy the error code to Pycharm, and the unwelcome character got busted.

enter image description here

So solution, for Mac user, be careful of of just simply copying the text from folder system, if something obviously weird, try type every letter into the text editor.

Jason
  • 3,166
  • 3
  • 20
  • 37
0

Specifying the absolute path using the os worked in windows

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

dont forget to import os though

Chikwado
  • 451
  • 6
  • 7