-1

I've been trying to access a folder outside my current folder, however when I Use "../images/image.png" it gives a FileNotFound error. Now running this code in IDLE gives no error and works perfectly. How can i use ../ in Vs code?

Using pygame.display.set_icon(pygame.image.load(f"./images/ui/logo.png")) which gives :- FileNotFoundError: No such file or directory. running it in IDLE 3.9, doesnt give me any error

User
  • 15
  • 3
  • 1
    Almost certainly because the current working directory in VSCode is not what you think it is. – BoarGules Jun 26 '22 at 12:03
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jun 26 '22 at 13:26
  • using `pygame.display.set_icon(pygame.image.load(f"./images/ui/logo.png"))` which gives :- `FileNotFoundError: No such file or directory`. running it in IDLE 3.9 doesnt give me any error @BoarGules – User Jun 26 '22 at 14:11
  • I realize that. I suggest you do `print(os.getcwd))` in each of the two environments, at the point where you are trying to open the file, and compare what they say. If they are not the same then `..` means different things. – BoarGules Jun 26 '22 at 14:18
  • They Are different! running `getcwd()` in IDLE gives :- `C:\Users\name\Desktop\Programs\Python\PyGames\test\rewrite\code` while in vs code:- `C:\Users\name\Desktop\Programs` @BoarGules – User Jun 26 '22 at 14:26
  • There really wasn't any other explanation for the behaviour you reported. As I said, it was almost certainly because the current working directory in VSCode was not what you thought it was. A reason to avoid relative paths where possible. Much better to make file locations a configuration setting. – BoarGules Jun 26 '22 at 15:01

2 Answers2

0

I am assuming that both folders are in the same directory. This means that you should use “./“ instead of “../“ :)

  • Hey, i tried it but it didnt work im using `pygame.display.set_icon(pygame.image.load(f"./images/ui/logo.png"))` which gives :- `FileNotFoundError: No such file or directory.` running it in IDLE 3.9 doesnt give me any error – User Jun 26 '22 at 14:10
  • I think you have no good reason to make that assumption. And OP wants a file *outside* the `cwd`, not *in* it. – BoarGules Jun 26 '22 at 14:13
0

The first thing to understand is that if the code in vscode involves paths, everything is retrieved for the root directory based on the workspace. Even if you use "." in a deep directory under the workspace, this still represents the workspace directory, instead of the current directory.

So what we need to do is to change our thinking and import the files you want with the workspace as the root directory.

This also means that ".." will not appear in vscode. Because all the files we need are in the workspace, we will not use the parent directory outside the workspace.

For example,

"./images/ui/logo.png"

enter image description here

If you place it like this, you can retrieve the file anywhere in the workspace without complex positioning based on the current file.

You can refer to this issue. It has similar problems with you.

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13