Val Kobilaski
Val Kobilaski

Reputation: 51

Trouble opening files in python with VS Code

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())

Upvotes: 5

Views: 23456

Answers (3)

misterlinkloquendo19
misterlinkloquendo19

Reputation: 61

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!

Upvotes: 1

5war00p
5war00p

Reputation: 408

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

Upvotes: 6

kett
kett

Reputation: 957

Try setting

"python.terminal.executeInFileDir": true,

in your VSCode Settings

Upvotes: 15

Related Questions