-5
root.iconbitmap("C:\User\user\Desktop\Pycharm\dragon.ico")

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 8-9: truncated \uXXXX escape

Md Ejaz
  • 3
  • 1
  • 3
    Welcome to StackOverflow, please please read this guide on [how to ask](https://stackoverflow.com/help/how-to-ask) – William Baker Morrison Dec 20 '20 at 15:50
  • 1
    "\" is a special character in a string used for escaping, like "\n". "\U" or "\u" is used for inputting Unicode character. So your your case, use raw string presentation `r"C:\User\user\..."`. – acw1668 Dec 20 '20 at 15:52

1 Answers1

0

Answer:
The problem is with the string "C:\User\user\Desktop\Pycharm\dragon.ico"

In the string, \U in C:\Users starts an eight-character Unicode escape. In your code, the escape is followed by the character 's', which is invalid.

There are two solutions:

  1. Duplicate all backslashes:
    "C:\\User\\user\\Desktop\\Pycharm\\dragon.ico"
  2. Add the prefix r to the string, to transform it into a raw string:
    r"C:\User\user\Desktop\Pycharm\dragon.ico"

Additional Links / References:
"Unicode Error "unicodeescape" codec can't decode bytes... Cannot open text files in Python 3

DapperDuck
  • 2,460
  • 1
  • 7
  • 20