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.
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.
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.
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.
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
Related
I am working on python tesseract package with sample code like the follows:
import pytesseract
from PIL import Image
tessdata_dir_config = "--tessdata-dir \"/opt/homebrew/Cellar/tesseract-lang/4.1.0/share/tessdata/\""
image = Image.open("dataset/test.jpeg")
text = pytesseract.image_to_string(image, lang = "chi-sim", config = tessdata_dir_config)
print(text)
And I received the following error message:
pytesseract.pytesseract.TesseractError: (1, 'Error opening data file /opt/homebrew/Cellar/tesseract-lang/4.1.0/share/tessdata/chi-sim.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory. Failed loading language 'chi-sim' Tesseract couldn't load any languages! Could not initialize tesseract.')
From my understanding, the error occurred when reading the file chi-sim.traineddata (which stands for Simplified Chinese), as I will explain the attempts I have made to settle this problem below.
My developing environment is M1 macOS, and I installed tesseract and tesseract-lang from Homebrew. I am pretty sure that the path specified above is exactly where the source files are located, since when I call
print(pytesseract.get_languages(config = ""))
I get a long list of languages printed, including chi-sim.
Further, if we just use English instead of Chinese, the following code can successfully recognize the English texts in an image:
text = pytesseract.image_to_string(image)
I've tried to specify environment variable TESSDATA_PREFIX in multiple ways, including:
Using config parameter as in the original code.
Adding global environment variable in PyCharm.
Adding the following line in the code
os.environ["TESSDATA_PREFIX"] = "tesseract/4.1.1/share/tessdata/"
Adding the following line to bash_profile in terminal
export TESSDATA_PREFIX=/opt/homebrew/Cellar/tesseract-lang/4.1.0/share/tessdata/
But unfortunately, none of these works.
It seems as if my file chi-sim.traineddata is, somehow, broken, so I directly downloaded the trained data file from GitHub (https://github.com/tesseract-ocr/tessdata/blob/master/chi_sim.traineddata), hit the "Download" button on the right, and placed the downloaded file in the tesseract-lang and original tesseract directory (where eng.traineddata is located). Yes, I've tried both, but neither works.
With respect to this issue, is there any potential solutions?
Code works for me on Linux if I use lang="chi_sim" with _ instead of - because file downloaded from server has name chi_sim.traineddata also with _ instead of -.
If I rename file into chi-sim.traineddata then I can use lang="chi-sim" (with - instead of _)
I am getting this error sometimes when working on mac in Processing for Python. Seemingly for no reason, sometimes the current working directory becomes what you see in the image while other times it is the working directory of the folder the pyde file is in as it should be.
Any ideas on why this is occurring?
It's to avoid problems like these that I always try to use absolute paths. I would suggest you try something like this for file paths:
import os
# This will be the path to your .py file
FILE_PATH = os.path.dirname(os.path.abspath(__file__))
# This will be the path to your text file, if it is in the same directory as the .py
LEVELS_FILE_PATH = os.path.join(FILE_PATH, "levels.txt")
Then, instead of your current open statement you could have:
f = open(LEVELS_FILE_PATH, 'r')
I'll try to be as simple as I can be. I'm not great at these things.
On my computer, at the location "C:\Users\Oria" there's a folder called Project. That folder contains code.ipynb, and a folder called data. Inside the folder data, there's just one file called iris_features.csv
I uploaded code.ipynb to jupyter notebook, there's a line there (which is locked to changes, can't change it) which reads
irisCsvFileName = 'data' + os.sep + 'iris_fearures.csv'
df_iris_features = pd.read_csv(irisCsvFileName)
So from what I understand, it should understand that the working directory is "C:\Users\Oria\Project" and all paths will be relative to it.
However, it doesn't work. It gives the error
FileNotFoundError: [Errno 2] File data\iris_fearures.csv does not exist: 'data\\iris_fearures.csv'
When I give the full path of the iris_features.csv, it works fine. However, as I said, I can't change the given code.
What am I doing wrong? Should I upload more than just the ipynb file to jupyter notebook?
There's a typo in the code you've provided in your question:
irisCsvFileName = 'data' + os.sep + 'iris_fearures.csv'
df_iris_features = pd.read_csv(irisCsvFileName)
You've written iris_fearures.csv but later have said that the file is called iris_features. You can check your current working directory is what you expect using:
import os
cwd = os.getcwd()
And you can find more information on using file paths etc in this SO answer
you have to give the full path if you didn't open the jupyter-notebook from the folder C:\Users\Oria\Project, if you just open a .ipynb from same folder Project the paths will not be relative to that .ipynb but with the folder from where you start the jupyter
you can check the current working directory (to whom all the other paths are relatives if they are not full paths):
import os
os.getcwd()
My code below, Python can't read my file:
f = open('./resource/review.txt','r')
The error is as in photo: Errno 2: No such file in directory.
You can try \ instead of /
f = open('.\\resource\\review.txt','r')
content = f.read()
print(content)
Because the file is not located at the same folder as your script you need to go back one folder.
In order to do that tou need '..' (double dot) and not one, this will indicate to go back a folder relative to the current path.
The message Errno 2: No such file in directory simply means that your relative pathname to your file cannot be found in the directory in which you're executing (which might not be the directory the Python script is in or that you think you're in, if you're using an IDE).
So to see what directory your Python is actually running in, do:
print(f'current directory is: {os.getcwd()}')
I launched Jupyter Notebook, created a new notebook in python, imported the necessary libraries and tried to access a .xlsx file on the desktop with this code:
haber = pd.read_csv('filename.xlsx')
but error keeps popping up. Want a reliable way of accessing this file on my desktop without incurring any error response
This is an obvious path problem, because your notebook is not booted on the desktop path, you must indicate the absolute path to the desktop file, or the relative path relative to the jupyter boot directory.
You will need to enter the full path of your excel file.
First:
Open your excel file, right click on the file and click on "Copy path to clipboard".
Second:
Next paste your path in your script. Mine looks something like this:
#only using one backslash "\"
'C:\Users\...YourFileName.xlsx'
Third:
You will likely have to modify this path by adding two "\" instead of one "\" in each spot you only see one backslash.
For example, my new path would now look like this:
#using two backslashes now "\\"
'C:\\Users\\...YourFileName.xlsx'
An example of your final output will look like this:
haber = pd.read_csv('C:\\Users\\...YourFileName.xlsx')
If you are using linux
/home/(your user name)/Desktop/(your filename)
if you are on windows
C:\Users\(your user name)\Desktop\( your filename)
and if your python file is on same path where dataset file is then just give the file name with extension