I write in CMD:
C:\ProjectStray\Model>python smth.py
But get:
python: can't open file 'smth.py': [Errno 2] No such file or directory
python C:\ProjectStray\Model\smth.py also not works
My files:
On another windows environments that works.
How can I fix that?
Related
I use subprocess.popen in python to put files in hdfs. it runs accurately using python on the Windows cmd. but as I use vscode to run the code, I get "FileNotFoundError: [Errno 2] No such file or directory: Error.
hdfs_path = os.path.join(os.sep,'mongo_import_export')
#put csv into hdfs
put = Popen(['hadoop','dfs','-put','mongo-hadoop/import_export.csv','/mongo_import_export'], stdin=PIPE,bufsize=-1)
put.communicate()
Knowing that my file import_export.csv is in the file in witch the code is located and mong-hadoop folder is in my local files
VSCode is running the code in a different working directory than your local CMD. Use the absolute path to the files you want to put rather than relative paths.
I had a working python file with code
form_class = uic.loadUiType("PGUI.ui")[0]
but I moved both .py file and .ui file to another directory and it doesn't work in VSCode with Error:
FileNotFoundError: [Errno 2] No such file or directory: 'PGUI.ui'
but it work when I make it .exe file with pyinstaller
How can I fix the problem?
It was my mistake about VSCode usage.
When I open folder A which has A\B\abc.py and A\B\PGUI.ui,
working directory is A, so abc.py cannot refer PGUI.ui
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 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