0

So i get this error

image = pygame.image.load("C:\Users\Dell\Downloads\spaceship.png")
                          ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

I did it with the exact path, and just with files name, but i still can't add it.

Fareed Khan
  • 2,613
  • 1
  • 11
  • 19
pizza
  • 1
  • 1
    You have to escape the backslashes. That means replacing \ with \\ in your string should solve the problem – Deepthought Jun 21 '20 at 12:19
  • 1
    try this `pygame.image.load("C:/Users/Dell/Downloads/spaceship.png")` – Fareed Khan Jun 21 '20 at 12:22
  • Does this answer your question? ["Unicode Error "unicodeescape" codec can't decode bytes... Cannot open text files in Python 3](https://stackoverflow.com/questions/1347791/unicode-error-unicodeescape-codec-cant-decode-bytes-cannot-open-text-file) – Ivo Mori Jun 21 '20 at 12:38
  • Before asking a new question it's always best to search for already existing Q/As on Stack Overflow and on other online resources. For example, if you search Stack Overflow with your error message you'll find similar (and already answered) questions immediately. – Ivo Mori Jun 21 '20 at 12:43

1 Answers1

0

The problem is that \ in strings is used for special chars like \n (break).

You can solve this either in this way:

image = pygame.image.load("C:\\Users\\Dell\\Downloads\\spaceship.png")

or this way:

image = pygame.image.load(r"C:\Users\Dell\Downloads\spaceship.png")
Deepthought
  • 118
  • 9