2016-08-26 51 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")