'VSCode file not found for file in current directory

I have a basic python script to open a file. The script and the file being opened are in the same folder.

If I use the command line in the folder, it works just fine, but from VS Code I'm getting a File not found error.

enter image description here

I have set via the preferences the CWD and it put this in my settings.json

    "python.testing.cwd": "${fileDirname}",

From the terminal window in VS Code when I run the code (F5) I can see it's even changing directory to where the program is.

(base) C:\Users\Neil>cd "e:\DATA\Projects\Modelling\Long Drop Data" && cmd /C E:\DATA\Dev\MiniConda\python.exe c:\Users\Neil\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\ptvsd_launcher.py --default --client --host localhost --port 50397 "e:\DATA\Projects\Modelling\Long Drop Data\read_data.py" "

Can anyone help?



Solution 1:[1]

You can either specify the current working directory you want in your launch.json or you will need to calculate the location of the file you want to read more concretely, e.g. pathlib.Path(__file__).with_name(filename) if the file is kept next to the Python file. (The testing setting only affects when you run tests via the extension.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brett Cannon