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__))
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.
Let's say I have the python file ~/file_to_run.py that I want to run from jupyter notebook (~/notebooks/my_notebook.ipynb) using the magic command %run. The problem is that file_to_run.py uses a relative path for example:
open('data/file.csv') # full path ~/data/file.csv
When I run ~/file_to_run.py from ~/notebooks/my_notebook.ipynb with:
%run ../file_to_run.py
I get the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'data/file.csv'
Is the any fix without modifying the python file? Thank you!
Changing the working directory could be a solution:
import os
os.chdir('../') # Change the working directory
%run file_to_run.py # Call the script from new working directory
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"
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
I'm trying to open a file in python. Simple enough. The script that I am using is the same directory as my code, so I just use
myfile = open('file.txt', 'r')
This worked fine before, but now I am getting the error 'No such file or directory' (Errno2)
Why is this happening? I've used OS to check if I am in the right directory, and it is fine. What is python doing differently now than it did 20 minutes ago, when it found the file perfectly??
Assuming the file you are trying to open/read has appropriate permissions, the behavior is defined based on how you are invoking your python program. Let's assume your code and the file.txt are in ~/Desktop
If you are in ~/Desktop and do a python code.py your code will work fine. But if you are in say your home folder - ~ and do a python ~/Desktop/code.py then the python interpreter assumes your current working directory to be ~ and will return the error:
IOError: [Errno 2] No such file or directory: 'file.txt'
since it will not find file.txt in ~
Further, in the context of the given example:
os.getcwd()
returns the absolute path of your home directory and
os.path.realpath(__file__)
returns the absolute path of your python source file
Is it possible you are typing the name wrong, eg "test.fna" versus "test.fna.txt"?