MrJoe
MrJoe

Reputation: 445

VSCode - open file in cwd

My VS code debug folder is: C:\Users\EML\Python Code

and the file I am working with is in: C:\Users\EML\Python Code\Boggle In the same folder I have the file en-dict.txt but I get this error message

with open("en-dict.txt", "r", encoding="utf8") as file:
FileNotFoundError: [Errno 2] No such file or directory: 'en-dict.txt'

This is my launch.json settings in VS Code:


    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal",
        "cwd": "${fileDirname}"
    }

I have looked at many StackOverflow answers and none have helped me yet.

Upvotes: 0

Views: 303

Answers (2)

Brett Cannon
Brett Cannon

Reputation: 16020

Try opening C:\Users\EML\Python Code\Boggle in VS Code instead of C:\Users\EML\Python Code as that will implicitly make the working directory be what you're expecting. Otherwise you have to be very careful about what file you have open when you launch the debugger to make sure that ${fileDirname} gets set appropriately. Otherwise you can hard-code the working directory in your launch.json.

Upvotes: 1

bracco23
bracco23

Reputation: 2211

if you set your launch as:

{
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": ""
        }

it will invoke the python in the same directory where it is located.

Upvotes: 2

Related Questions