Reputation: 83
In a project I'm working on, I have to load a few dozen images. However if i try to load any of them, like this:
twoc = pygame.image.load("C:\Users\Z & Z Azam\AppData\Local\Programs\Python\Python35\Scripts\Cards\2_of_clubs.png")
I get this message:
"C:\Users\Z & Z Azam\AppData\Local\Programs\Python\Python35\python.exe" "C:/Users/Z & Z Azam/Desktop/New folder (2)/Python Stuff/PycharmProjects/ProjectGambler/Graphics-Cards.py"
File "C:/Users/Z & Z Azam/Desktop/New folder (2)/Python Stuff/PycharmProjects/ProjectGambler/Graphics-Cards.py", line 16
twoc = pygame.image.load("C:\Users\Z & Z Azam\AppData\Local\Programs\Python\Python35\Scripts\Cards\2_of_clubs.png") # Lines 15-66 bring all the cards into the program
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
I have no idea what I did wrong. Is there anyone who can help me?
UPDATE: So I've gone and replaced every \ in the file addresses with /. Again, it works perfectly until:
"C:\Users\Z & Z Azam\AppData\Local\Programs\Python\Python35\python.exe" "C:/Users/Z & Z Azam/Desktop/New folder (2)/Python Stuff/PycharmProjects/ProjectGambler/Graphics-Cards.py"
Traceback (most recent call last):
File "C:/Users/Z & Z Azam/Desktop/New folder (2)/Python Stuff/PycharmProjects/ProjectGambler/Graphics-Cards.py", line 28, in <module>
fivc = pygame.image.load("C:/Users/Z & Z Azam/AppData/Local/Programs/Python/Python35/Scripts/Cards/5_of_clubs")
pygame.error: Couldn't open C:/Users/Z & Z Azam/AppData/Local/Programs/Python/Python35/Scripts/Cards/5_of_clubs
Process finished with exit code 1
Upvotes: 1
Views: 1037
Reputation: 23233
Minimal example that exposes the error:
s = "\U"
In Python backslash is used as escape sequence and \Uxxxx
pattern is used to declare unicode characters while keeping source code ascii-only. By using \Users
in your string, string after \U
is invalid hex number, therefore exception is raised.
Quickest fix - mark string as raw:
s = r"C:\Users\Z & Z Azam\AppData\Local\Programs\Python\Python35\Scripts\Cards\2_of_clubs.png
Upvotes: 2
Reputation: 22963
Basically you need to add a escape slash the other slash in your string. So you will end up with two slashes, like so:
twoc = pygame.image.load("C:\\Users\\Z & Z Azam\\AppData\\Local\\Programs\\Python\\Python35\\Scripts\\Cards\\2_of_clubs.png")
Or the other way you go about this, is to change your backslash to a forward slash:
twoc = pygame.image.load("C:/Users/Z & Z Azam/AppData/Local/Programs/Python/Python35/Scripts/Cards/2_of_clubs.png")
Upvotes: 1