I get this errors quite often when trying to run a python file using CMD :
Can't open : [Errno 2] No such file or directory.
I have tried both: cd <path> where path is folder where file is saved; and python <path\filename> . Both give the exact same error.
How to get rid of it?
Related
I am trying to open a .dat file by Pickle. Although the file is at the same folder as my .py file, when I run I get a FileNotFoundError.
(FYI: I am using VScode to run the file, however, it runs perfectly fine when I use Terminal)
here is my code
import pickle
websites_list = pickle.load(open("websites.dat","rb"))
print(websites_list)
This is the .py path:
/Users/lequangdang/Documents/WSC/WSC_2.0/changewebsiteGUI.py
here is the .dat path: /Users/lequangdang/Documents/WSC/WSC_2.0/websites.dat
Here is the error:
FileNotFoundError: [Errno 2] No such file or directory: 'websites.dat'
The relative path is base on work directory when using vscode, but is base on py file itself when using terminal.
For example the directory structure seems like:
WSC---WSC2.0---changewebsiteGUI.py
when you open WSC in vscode, pickle.load(open("websites.dat","rb")) will call the path WSC/websites.dat and that's why you have FileNotFoundError
one way to solve this is to set the vscode launch.json, add this line:
"cwd": "${workspaceRoot}/WSC2.0"
the other way is to use some function to get the abs path of pyfile itself:
os.path.dirname(os.path.abspath(__file__))
I am running python file from command line. To be more specific my .py file is in location F:\new_github_code\cocktail_party\data\audio\audio_downloader.py>. I want to run audio_downloader.py file from the cocktail_party directory not from the audio directory.
I tried by using the following script from cmd. Any help will be appreciate
enter image description here
Try this. It will work:
python data\audio\audio_downloader.py
I installed Sublime Text 3 notepad to write Python3 and to see the .py files on true location i runned myexample.py (which is on desktop and just printed hello world) on CMD
cd Desktop
python myexample.py
then I got an error message like
python: can't open file 'myexample.py': [Errno 2] No such file or directory
it has to show me only
hello world
on cmd screen instead of that error. What might be wrong here?
I tried to write python3 on cmd, I checked the locations and the myexample.py is on desktop, i even tried to write anaconda to the cmd but couldn't solve it.
python: can't open file 'myexample.py': [Errno 2] No such file or directory
python: can't open file 'Test.py': [Errno 2] No such file or directory even though I have specified the working directory of the container.
CMD path
Try using full path when you try to run something.
/home/user/Test.py
I want to run python command as follows, without the whole path of pyahp or other scripts. It reported the error. I have to add the whole path, ie .../site_packages/ that is tedious. How can I run it directly?
[~] python pyahp
/Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python: can't open file 'pyahp':
[Errno 2] No such file or directory
I trid to add pythonpath, however it failed.
export
PYTHONPATH="$PYTHONPATH:/Library/Frameworks/Python.framework/Versions/3.6/lib"