keddad
keddad

Reputation: 1796

Python script can't find file when opening with VS code, but works OK with terminal

I've got a python script which works with some files using normal

with open("input.txt", "r") as file:

And there is input.txt in the same folder. Problem is, when I launch it with terminal, like

keddad@keddad-pc:~/bioinformatics-algorithms/1.3/PatternMatcher$ python3 ~/bioinformatics-algorithms/1.3/PatternMatcher/main.py

It works well, but when I try to run it with VS Code Debugger/Without debugger, it just can't find the files:

keddad@keddad-pc:~/bioinformatics-algorithms$ cd /home/keddad/bioinformatics-algorithms ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /usr/bin/python3 /home/keddad/.vscode/extensions/ms-python.python-2019.8.30787/pythonFiles/ptvsd_launcher.py --default --nodebug --client --host localhost --port 46499 /home/keddad/bioinformatics-algorithms/1.3/PatternMatcher/main.py 
Traceback (most recent call last):
  "some traceback here"
  File "/home/keddad/bioinformatics-algorithms/1.3/PatternMatcher/main.py", line 19, in <module>
    main()
  File "/home/keddad/bioinformatics-algorithms/1.3/PatternMatcher/main.py", line 10, in main
    with open("input.txt", "r") as file:
FileNotFoundError: [Errno 2] No such file or directory: 'input.txt'

How can I make VS Code launch my scipts in a way that they find the files in the same directories?

Upvotes: 8

Views: 11793

Answers (2)

rioV8
rioV8

Reputation: 28623

In the launch config change the CWD to the folder

{
    "version": "0.2.0",
    "configurations": [
        {
            ....,
            "cwd" : "${workspaceFolder}/${relativeFileDirname}"
        }
    ]
}

Upvotes: 20

user2672299
user2672299

Reputation: 423

Your terminal in vscode is at this location: "~/bioinformatics-algorithms"

This is were the terminal executes the python code and search for the files. You have to change the terminal location to "~/bioinformatics-algorithms/1.3/PatternMatcher".

Upvotes: 1

Related Questions