hi i have the following code in my main.py file
store = JsonStore(r'save/budgets/{}'.format(budgetName))
store.put('budget', items= ['soap','drinks'], total=10)
Where "budgetName" is gotten from a textinput entry.
"save" is a folder in the same directory as my main.py,
and "budgets" is a folder found in the "save" directory,
the plan is to create the .json file in the budgets directory.
This used to work when i tested on kivy launcher on my android phone. But once i created an apk,
it crashes the program with the error exception
Errno 2: No such file or directory
please how do i fix this
Related
I am trying to use Pillow Library in Python. I am writing basic code to open up a picture that I have saved.
But every time I run the code, it says
"can't open file 'C:\Users\saadn\PycharmProjects\Giraffe\main.py':
[Errno 2] No such file or directory".
The path for where the picture is saved is "C:\Users\saadn\PycharmProjects\Giraffe\DirectoryAssign1"
Now I know that in the error it says "Girrafe\main.py". but I don't have a python file named main at all. Someone guide me.
the code I wrote was
from PIL import Image
img = Image.open("dany.jpg")
img.show()
Here is a screenshot for better understanding.
Actually I think there are 2 issues, and not 1.
Error 1 - Wrong File is being tried to run :
In the picture you attached there is an error which means it cannot open main.py (No such file) . You entered a wrong file name, your file name is OpenAndShow.py
How to fix?
Be sure to open the terminal or command prompt and be sure you are in the following directory C:\Users\saadn\PycharmProjects\Giraffe\ and enter the following command as you are a Windows OS User:
py OpenAndShow.py
Fun fact: Of course Python won't search in the whole PC or directory (including folders which are in the directory) , so that's why you need to mention it is in a folder in the Giraffe folder!
Error 2 - Wrong location for the picture
If you had fixed error 1, you will get another error, that dany.jpg is not such file, why ? Because it is in another folder inside the folder your code is located, you can use dany.jpg when they are in the same folder, not in another folder in the same folder, in this case, it is in DirectoryAssign1 which is in Girrafe folder.
How to fix?
To fix it, change the path:
From : The current Path:
dany.jpg
To : The following path below:
DirectoryAssign1\dany.jpg
You are running the wrong file. In PyCharm, afaik you can right click on the file and run. Your PyCharm is now trying to run the file main.py.
Otherwise, you can use the terminal:
python OpenAndShow.py
I have created my python script and linked in tkinter UI both .py files are running fine when i ran it visual studio code. But I have converted tkinter UI .py file to .exe file to run my python script in executable file. I have successfuly converted the executable file. But when i run through the executable file(Application) No Such file in directory error coming
'c:\users\username\appdate\local\temp_MEI16402\myprogram.py' no such file or directory
This is temp location
But i have my UI.py,myprogram.py in same location
how to solve this error. Please help.
To import myprogram.py into UI.py you can do this:
import myprogram.py
The when you build it into an .exe file with pyinstaller you can add the --onefile option to build both scripts into one file. Here's an example:
pyinstaller --onefile UI.py
I created a simple hello world text document named "hello.py" stored in a directory in a drive. I then opened cmd, navigated to the directory where the document was stored, and typed in
py hello.py
And then cmd gives me this error:
C:\Users\nufil\AppData\Local\Programs\Python\Python39\python.exe: can't open file 'D:\Python\hello.py': [Errno 2] No such file or directory
How do I rectify this error and make cmd run my code?
I am working through Learn Python the Hard Way but am having a problem with an exercise that reads a file: https://learnpythonthehardway.org/python3/ex15.html
I have followed this and made sure my file is in the same folder as my python file. I keep getting the error:
Traceback (most recent call last):
File "ex15.py", line 5, in <module>
txt = open(filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ex15_sample.txt'
I am entering: python ex15.py ex15_sample.txt into the powershell. I tried looking up the problem I'm having but could not find anything that helped in my case. Let me know if I am doing something wrong or if I'm missing something. Thanks
You can check your current path and whether your file exists by
import os
#get the current working directory
os.getcwd()
#check whether your file exists in the current working directory
os.path.exists("./ex15.py")
try 2 things below
Check both files .py and .txt are at same location. and then execute program.
this is how i ran the code.
C:\Users\db\Desktop\Python>python ex15.py
enter File Name: ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
If your file is at other location you need to provide absolute path for your file. as
C:\Users\db\Desktop\Python>python C:\Users\deepakb\Desktop\ex15_sample.txt
i had my txt file at desktop this time.
A brief demonstration on working directory and relative file locations..
Say we have a text file named hello.txt with the contents:
Hello World!
that is located at "C:\Users\username\Documents\python\hello.txt"
And we have a python script named script.py with the contents:
with open('hello.txt', 'r') as f:
print(f.read())
Which is located at "C:\Users\username\Documents\python\script.py"
If I just hit the windows key and type power shell and hit enter to open it, I'll see it by default gives me a prompt at my home directory: "C:\Users\username>". From here I can execute my python script by calling C:\Users\username> python C:\Users\username\Documents\python\script.py. If I do this, however, they python interpreter would have started in my home folder, and when it tries to find "hello.txt" it will only search through the folder "C:\Users\username". That's not where the file is, so we get a file not found error.
To resolve this we have a few options. Number one is to re-write our script file to include the full path of our text file: ... open("C:/Users/username/Documents/python/hello.txt", ... This way there is no searching necessary, and the script can be run from anywhere. Another option is to change the working directory of the interpreter from within the script. You can set the working directory with the os library by calling os.chdir('C:/Users/username/Documents/python'). Finally you can set the working directory with PowerShell before calling the python interpreter using the cd command:
C:\Users\username> cd .\Documents\python\
I had the same error.
It was a mistake from me naming the file as ex15_sample.txt.
When you are creating a file in notepad, the file name would be automatically .txt
so if you have created a file just rename it as ex15_sample.
or
You can also run the same program by giving python ex15.py ex15_sample.txt.txt
As you can see the file name might be ex15_sample.txt with a .txt extension.
Both files, the .py file and a .txt file it calls with
champions_list = open('champions.txt','rU').read().split('\n')
are in the file folder C:\Users\[My Name]\Programming\[file name].
I'm calling the .py file through Command prompt and it returns the error
IOError: [Errno 2] No such file or directory: champions.txt
Has this happened to anyone else before?
When you open a file with open('champions.txt'), then the OS expects to find the champions.txt file in the current directory. The current directory is the directory of the command prompt window where you started the program. This is not (necessarily) the same as the directory where the Python script is stored.
You may be able to fix this by doing:
import os
import sys
open(os.path.join(os.path.dirname(sys.argv[0]), 'champions.txt')
This takes the full name of the script in sys.argv[0], takes the directory part, and then joins that to the file name you want. This will open the file in the script directory, not the current directory.
(Note that using sys.argv[0] in this way is OS-dependent, and works on Windows but may not work the same way on other systems.)
Just because the file is in the same folder as the script does not mean the python interpreter knows that the file is there. It is looking for a file in the cwd. You can:
Try using the full absolute path of the file; or
Add the directory containing the file using os.path.append