(Unicode Error) 'Unicodeescape' Codec Can't Decode Bytes in Position 2-3: Truncated \Uxxxxxxxx Escape

Unicode Error unicodeescape codec can't decode bytes... Cannot open text files in Python 3

The problem is with the string

"C:\Users\Eric\Desktop\beeline.txt"

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

You either need to duplicate all backslashes:

"C:\\Users\\Eric\\Desktop\\beeline.txt"

Or prefix the string with r (to produce a raw string):

r"C:\Users\Eric\Desktop\beeline.txt"

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

\ has special meaning in python strings, so you need escape it by doubling to get literal \, all \ in all paths should be replaced using \\, for example

C:\Users\Danny\DiscordExports\log.txt

should be

C:\\Users\\Danny\\DiscordExports\\log.txt


Related Topics



Leave a reply



Submit