Python Forum
(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \
#2
Never use use single backslash (\) that way in path,because of escape character.
\U starts an eight-character Unicode escape in Python 3.
Error:
>>> s = 'C:\Users\DSule\Desktop\python\nyc.csv' Traceback (most recent call last): ......... SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Here are three ways that work.
>>> # Raw string(r)
>>> s = r'C:\Users\DSule\Desktop\python\nyc.csv'
>>> s
'C:\\Users\\DSule\\Desktop\\python\\nyc.csv'

>>> # Other way /
>>> s = 'C:/User/DSule/Desktop/python/nyc.csv'
>>> s
'C:/User/DSule/Desktop/python/nyc.csv'

>>> # Double up
>>> s = 'C:\\Users\\DSule\\Desktop\\python\\nyc.csv'
>>> s
'C:\\Users\\DSule\\Desktop\\python\\nyc.csv'
Afy555 likes this post
Reply


Messages In This Thread
RE: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \ - by snippsat - May-11-2018, 10:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Fatal Python error: initfsencoding: unable to load the file system codec gauravbhardwajee 12 28,367 Apr-30-2020, 07:45 PM
Last Post: barrpath

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020