Moataz Harrabi
Moataz Harrabi

Reputation: 11

How do I fix visual studio code path error?

so I have this problem in visual code, when I open a file in the same directory of a project, visual code doesn't identify the file unless I give him the whole directory

This is the image of the error: This is the image of the error

Upvotes: 1

Views: 8386

Answers (2)

Steven-MSFT
Steven-MSFT

Reputation: 8411

cwd:

Specifies the current working directory for the debugger, which is the base folder for any relative paths used in code. If omitted, defaults to ${workspaceFolder} (the folder open in VS Code).

And you can set it to:${fileDirname} - the current opened file's dirname(official docs)

Upvotes: 1

rzlvmp
rzlvmp

Reputation: 9364

Try this way to set working directory manually:

/tmp/1/
/tmp/2/file
import os

print("Current working directory: {0}".format(os.getcwd()))

> Current working directory: /tmp/1

open('file', 'r')

> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> FileNotFoundError: [Errno 2] No such file or directory: 'file'

os.chdir('/tmp/2')

open('file', 'r')

> <_io.TextIOWrapper name='file' mode='r' encoding='UTF-8'>

Also you can set directory related to the running python file:

import os
print(os.path.dirname(os.path.abspath(__file__)))
python /tmp/1/file.py

> /tmp/1

The good practice is to set application path somewhere in global configuration file

Upvotes: 0

Related Questions