2016-08-26 56 views
1

在我正在处理的项目中,我必须加载几十个图像。但是,如果我尝试加载任何人,像这样的:语法unicode错误pygame

twoc = pygame.image.load("C:\Users\Z & Z Azam\AppData\Local\Programs\Python\Python35\Scripts\Cards\2_of_clubs.png") 

我得到这个消息:

"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 

我不知道我做错了什么。有没有人可以帮助我?

更新:所以我走了,用/替换文件中的每个\地址。再次,它完美的工作,直到:

"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 

回答

1

基本上你需要添加一个转义斜杠在你的字符串中的其他斜杠。所以,你最终会与两个斜杠,像这样:

twoc = pygame.image.load("C:\\Users\\Z & Z Azam\\AppData\\Local\\Programs\\Python\\Python35\\Scripts\\Cards\\2_of_clubs.png") 

或者你去这个问题的另一种方式,是你的反斜线更改为正斜杠:

twoc = pygame.image.load("C:/Users/Z & Z Azam/AppData/Local/Programs/Python/Python35/Scripts/Cards/2_of_clubs.png")