1

I have a problem with the pygame module, pygame.image.load() to be exact. And the problem is that no matter if I write the full directory of the png that I want to load (from C: until the file) or if I write part of the directory, or I just type the name of the png + extension (player.png) or even just player, it always gives me an error and here is the list

full directory: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

half of the directory: No such file or directory

just the name + extension: No such file or directory

just the name: No such file or directory

(And yes, I made sure that the program and the files are in the same folder and already checked multiple multiple websites like github or pygame.org itself)

hellwraiz
  • 359
  • 2
  • 13

1 Answers1

1

I'm not 100% sure, but it looks like the backslash is getting incorrectly interpreted. E.g.

'C:\User\john\xyz\...'

So I think that a backslash isn't getting interpreted as string and instead as sort of like a function.

If you try this

print("One Line \n Another Line")

You should see them outputted on separate lines.

POSSIBLE SOLUTION

It seems like a fix for your issue is to place an "r" before the speech marks when specifying the path.

file_location = pygame.image.load(r"C:\Users\john\pygame_folder\textures\exitdoor.png")

Alternatively, try replacing the backslashes with '/'

If this didn't help then try all this link (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Searching up your error code can help you find others who've gotten stuck in the same place.

  • Thank you very much! Didn't think that missing a letter would make me so frustrated for 2 days... and thanks for the advice! Next time I will make sure to research more thoroughly before asking something simple. – hellwraiz Apr 30 '21 at 22:40
  • hey, no problem. Everyone's gotta start somewhere. I actually didn't even know about using "r". I always used forward slash. good luck with your game! – dimthemainsjfdjlks May 01 '21 at 10:27