SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \uXXXX escape

SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \uXXXX escape

Code:

print("C:\users\t k\t est.txt")

Error:

Donot use strings directly while specifying the path with backward slash which will be considered by interpreter as escape characters

   print("C:\users\t k\t est.txt")
         ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape

Solution:

Use double backward slash while using strings with drive path

print("C:\\users\\tk\\test.txt")

Use ‘r’ in front of the drive path

print(r"C:\users\tk\test.txt")

Use forward slash

print("C:/users/tk/test.txt")

Leave a Reply

Discover more from XscalibaL

Subscribe now to keep reading and get access to the full archive.

Continue reading