0

I have a problem with importing a file like ../path/to/file.png I recieved this as an error message: FileNotFoundError: [Errno 2] No such file or directory. So as it seems like the current working directory gets booted in the folder "code". Then I need ../ to get to an folder thats in the same folder as the folder "code", for instance the folder "graphics" right? So, as I assume

f = pygame.image.load("../graphics/image.png")

should work to open my image with pygame. I am using the newest python and pygame version. I also use VScode with the python extension. So I hope I'll find some help here.

Thanks in advance.

Trissi17g
  • 39
  • 6

2 Answers2

1

Try this,

f = pygame.image.load(os.path.dirname(__file__) + '../graphics/image.png')

To know more about os.path.dirname(__file__) read this, https://www.geeksforgeeks.org/python-os-path-dirname-method/

To know more about __file__ read this, what does the __file__ variable mean/do?

Himanshu Kawale
  • 389
  • 2
  • 11
0

You don't need the .. because opening files happens in the root directory that your files are in anyways. so just put the graphics folder alongside the scripts you are running. If you are still having a problem please include an image or some sort of visual of what your project/files look like.