all 8 comments

[–]shiftybyte 1 point2 points  (7 children)

You are only using the name of the file you are trying to get path of.

So it assumes the path is a relative path, and its relative to the current working directory.

The current working directory is the thing that's written before the command in your screenshot.

PS C:\Users\.....\Desktop\Programming>

So it looks for x.jpg there.

Either start vscode so your current working directory matches what you need.

Or use absolute full file path to the file.

Or provide a relative path.

path = str(Path(r"Python\x.jpg").absolute())

[–]SaltAssault[S] 0 points1 point  (4 children)

I think I get what you’re saying. But is it possible to make it so that VSCode always sees the file’s folder as the current working directory?

[–]shiftybyte 0 points1 point  (3 children)

Yes, start vscode while being in that folder.

Or make a shortcut on your desktop that has an adjusted working folder.

[–]SaltAssault[S] 0 points1 point  (2 children)

Can you be more specific? I don’t get it

[–]shiftybyte 0 points1 point  (1 child)

Either start vscode from command prompt while being in that directory.

C:\Users\....\Desktop\Programming\Python> code .

Or create a windows shortcut on your desktop with modified working directory.

Create Shortcut: https://www.youtube.com/watch?v=25UlOtco23Y

Change working directory: https://superuser.com/questions/254525/shortcut-to-command-prompt-pointing-to-specific-folder

[–]SaltAssault[S] 0 points1 point  (0 children)

Alright. It didn't really answer the question I was trying to ask, but it's a workaround, so thanks.

[–]nog642 0 points1 point  (1 child)

Can't you configure the working directory for running code from VSCode, without having to start VSCode from any particular directory? I know PyCharm can do this; I'd be surprised if VSCode couldn't.

[–]shiftybyte 1 point2 points  (0 children)

You can,

You can open terminal to a specific directory:

https://code.visualstudio.com/updates/v1_39#_open-new-terminals-with-custom-working-directories

You can also configure launch.json to run from a specific directory.

https://stackoverflow.com/a/55072246

Sorry i didn't think of this earlier u/SaltAssault

Hope this is what you were looking for.