0

I have put my pictures and images all into a folder under my desktop. When I try to import it, it wont load. Below is my code:

walkRight = pygame.image.load('C:\Users\name\Desktop\Game - Copy\R1.png')

The error I am getting is below:

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

mkrieger1
  • 14,486
  • 4
  • 43
  • 54
OOF
  • 11
  • 1

1 Answers1

0

The character '\' in a string starts an escape sequence. Either use raw String literals (r"my string"):

walkRight = pygame.image.load('C:\Users\name\Desktop\Game - Copy\R1.png')

walkRight = pygame.image.load(r'C:\Users\name\Desktop\Game - Copy\R1.png')

Or use '/' instead of '\':

walkRight = pygame.image.load('C:\Users\name\Desktop\Game - Copy\R1.png')

walkRight = pygame.image.load('C:/Users/name/Desktop/Game - Copy/R1.png')
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
  • The problem with that line and 2 others is solved. But there is a line which is giving me a error although the file is in the same file and directory. – OOF Jan 04 '21 at 19:27
  • @OOF So you have a similar problem on this other line. Should we guess the error? – Rabbid76 Jan 04 '21 at 19:29
  • I dont know. But I have gone into google and stack overflow to find out what to do. But the options given i have already tried. So I don't know what is going on – OOF Jan 04 '21 at 19:37
  • @OOF What is the line that gives the error? – Rabbid76 Jan 04 '21 at 19:38
  • char = pygame.image.load('C:/Users/name/Desktop/Game - Copy/standing.jpg'). There is the code – OOF Jan 04 '21 at 19:39
  • @OOF Sorry this is not possible. Except that the file is corrupted? What is the exact error message? – Rabbid76 Jan 04 '21 at 19:44
  • FileNotFoundError: No such file or directory. What do you mean by this isn't possible – OOF Jan 04 '21 at 19:48
  • @OOF When you get the error message "No such file or directory", then it is impossible that the file exists. You have overlooked something. Is the file path correct? Is the file name correct? Is the file extension correct? Is there a typo? – Rabbid76 Jan 04 '21 at 19:49
  • 1
    I think I found the error. I gave the extension as jpg but it is png. But it is giving with another function – OOF Jan 04 '21 at 19:52