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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s