Reputation: 1189
When I load files via Visual Studio Code, VScode can't find directory.
I can run code without issue with terminal, result is:
young@young-desktop:/media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials/10_RNN/ChatBot$ python text_load_text.py
['fij\n', 'feijfaef\n', 'ef\n', 'awef\n', 'awe\n', 'g\n', 'aweg\n', 'ae\n', 'wg\n', 'awe\n', 'h\n', 'aw\n', 'h\n', 'aw\n', 'ef\n', 'aweg\n', 'wea\n', 'gaw\n', 'eg\n', '\n']
But with VScode:
young@young-desktop:/media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials$ cd /media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials ; env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" /home/young/anaconda3/bin/python /home/young/.vscode/extensions/ms-python.python-2018.6.0/pythonFiles/PythonTools/visualstudio_py_launcher.py /media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials 39707 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput /media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials/10_RNN/ChatBot/text_load_text.py
Traceback (most recent call last):
File "/media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials/10_RNN/ChatBot/text_load_text.py", line 8, in <module>
with open('test.txt', 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
What causes this issue?
test.txt file originally is intended to be located in ./data/test.txt
So I tested
with open('./data/test.txt', 'r') as f:
But it failed with VScode
So I tried to move test.txt file to working directory with trying:
with open('test.txt', 'r') as f:
and
with open('./test.txt', 'r') as f:
But all failed with VScode.
Upvotes: 12
Views: 69202
Reputation: 10490
By default, VS Code runs the program such that the current working directory is the workspace folder. So when the program is being run, the integrated terminal cd
to that folder (see this answer), and expects to find files over there.
To make the program run in the current working directory, we can add "cwd": "${fileDirname}"
to launch.json
file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
}
]
}
Now go back to your program file and run it (don't accidently try to run launch.json
itself, it won't work :).
Upvotes: 0
Reputation: 169
Try this: enable the below option in your VSCode python setting
When executing a file in the terminal, whether to use execute in the file's directory, instead of the current open folder
Upvotes: 16
Reputation: 159
First of all, get the directory of the module that is currently running by os.path.dirname(__file__)
.
Then, you can join the relative path of your desire file to this directory path.
from os.path import dirname, join
current_dir = dirname(__file__)
file_path = join(current_dir, "./test.txt")
with open(file_path, 'r') as f:
Upvotes: 12
Reputation: 1
just place the txt file in the same folder where the .py file you are running the code from is.
i.e my code is in dile.py which is in a folder "python program". Place the txt file in that folder
Upvotes: 0
Reputation: 318
Make sure you are in the directory that your .txt
file is on in your terminal or command prompt.
In your terminal cd
into the directory that holds the text
file and .py
file , run the program and it should work fine.
Upvotes: 0
Reputation: 242
I had an identical issue when trying to run a python script in VScode. Turns out, the function I needed to use was os.chdir('path')
, and not sys.path.append('path')
This issue was specific to VScode. VScode was running the script out of the wrong directory. I found out what directory it was in using print(os.listdir())
, and then changed the directory accordingly. Here's a snippet of the code that helped me resolve this.
import sys, os
print(os.listdir())
os.chdir('C:\\*****\\dir')
print(os.listdir())
Upvotes: 4
Reputation: 1189
When I open this project, I opened it as "TensorFlow-Tutorials" folder.
So my working directory was:
young@young-desktop:/media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials
So "." indicates TensorFlow-Tutorials path
In this reason, following path fails to find file
with open('./test.txt', 'r') as f:
Solution is adding additional part to indicate my real target directory precisely:
with open("./10_RNN/ChatBot/test.txt", 'r') as f:
x = f.readlines()
print(x)
Upvotes: 1