I want to open a file with python in vscode but there is a FileNotFoundError.
I have the txt-file in the same directory as the python file.
I deleted the file and than wrote it again in the right directory but it didn’t help.
The path is correct and it works when I use import os.
This is because you are not opening VSCode in the same directory as you have your codes. As you can see in the terminal, the path that you are running the code from is :
C:UsersRaoulDocumentsPythonVisual Studio Code
Where possibly in this path there is no story.txt file. Now what can you do to address this issue? There are two possible ways :
1. Without using terminal
You can go to the directory and right-click on an empty place in that directory and select Open with Code, then run your code :
2. Using terminal
You can navigate in terminal to the directory that your code is, then write :
code .
Which means open VSCode in this directory. Then run your code without any problem. There are more sources that you can read and learn this way of opening VSCode, like this link.
It most likely doesn’t work because of the folder from which you run the file. This folder will be used as the current working directory and therefore a local reference doesn’t work. You can try this by running it from the folder the script is in: "C:UsersRaoulDocumentsPythonVisual Studio Codeprojectsmini_projects_9h"
3
Answers
It seems that your working directory is C:UsersRaoulDocumentsPythonVisual Studio Code but your file reside in another directory.
Try changing the working directory to the same directory where your python and txt files reside.
Try this:
This is because you are not opening
VSCode
in the same directory as you have your codes. As you can see in the terminal, the path that you are running the code from is :C:UsersRaoulDocumentsPythonVisual Studio Code
Where possibly in this path there is no
story.txt
file. Now what can you do to address this issue? There are two possible ways :1. Without using terminal
You can go to the directory and right-click on an empty place in that directory and select
Open with Code
, then run your code :2. Using terminal
You can navigate in terminal to the directory that your code is, then write :
code .
Which means open VSCode in this directory. Then run your code without any problem. There are more sources that you can read and learn this way of opening VSCode, like this link.
It most likely doesn’t work because of the folder from which you run the file. This folder will be used as the current working directory and therefore a local reference doesn’t work. You can try this by running it from the folder the script is in: "C:UsersRaoulDocumentsPythonVisual Studio Codeprojectsmini_projects_9h"
This solution elaborates more on this: enter link description here