Issue
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.
Solution
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?
Answered By - Himanshu Kawale
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.