1
votes

I can't seem to open basic text files when I run a python script in VS Code, the code runs fine when I use it in Idle but VSCode will give me a FileNotFound error, is there some sort of extension that allows me to do this? Right now I'm only using the Python and Code Runner extension. example of my code:

def getInput(textFile):
    file=open(textFile,'r')
    print(file.readlines())
3
Are you using the absolute path to the file ? - JacobIRR
To add on to what @JacobIRR is asking, perhaps the execution directory is not what you think it is (and is different between IDEs, which would explain Idle vs Code discrepancies). If that's the case, adding the exact FileNotFound error and your directory structure will help us diagnose the issue. - Jules Dupont
@JacobIRR: file is not keyword for python 3. Just saying. - Rahul
@Rahul did not realize that, thanks - JacobIRR
In VS code relative path works. The code runner runs the file from the directory you are in. - Rahul

3 Answers

10
votes

Try setting

"python.terminal.executeInFileDir": true,

in your VSCode Settings

3
votes

In your VSCode for Windows

File --> Preferences --> Settings --> (User)Extensions --> Python -->

Terminal: Execute In File Dir --> Check It

or

File --> Preferences --> Settings --> Search Settings --> python.terminal.executeInFileDir

0
votes

Whenever you need to debug, you need to change the path of the file to open it in debug mode by simply:

  1. Going to the explorer
  2. Right click on the file and click on copy relative path
  3. Put that path in open

That should help you!