#๐ Path wont work
11 messages ยท Page 1 of 1 (latest)
@winged violet
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Hello @winged violet, what is the whole error message?
Remember to always show the whole error message, even if you're not sure that it helps.
d:\Project\code\Project.py:16: SyntaxWarning: invalid escape sequence '{'
self.image = pygame.image.load(f"{path}{image}.png")
pygame 2.6.1 (SDL 2.28.4, Python 3.13.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
<unknown>:2: SyntaxWarning: invalid escape sequence '{'
<unknown>:1: SyntaxWarning: invalid escape sequence '{'
File "d:\Project\code\Project.py", line 90, in <module>
player = Player(100, 100, "Idle1", path)
File "d:\Project\code\Project.py", line 16, in init
self.image = pygame.image.load(f"{path}{image}.png")
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: No such file or directory: 'd:\Project\code\game_images\Idle1.png'.
in ```py
self.image = pygame.image.load(f"{path}{image}.png")
- escape it in the string: `f"{path}\\{image}.png"` by adding another backslash, it makes it actually just one
- make it a raw string: `rf"{path}\{image}.png"` by adding `r` as another "string flag", it ignores all character escapes
Ty
generally for more cleaner code, i'd recommend utilizing pathlib instead of dealing with raw string paths
never worked with it and the project is not something ill continue working on so it isnt really a problem, but ill keep it in mind when ill be working on later projects
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.