BevA
BevA

Reputation: 1

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: malformed \N character escape

Apologies, I know there are similar threads, but I still haven't had any luck! I'm brand new to Python & trying to interpret a code that a colleague wrote.

I keep getting the Unicodeescape error:

SyntaxError: (unicode error) unicodeescape codec can't decode bytes in position 0-1: malformed \N character escape

I know it's something to do with the way Python interprets 'Users', but haven't had much luck adding r. or adding double slashes (I've probably put them in the wrong place!)

Any help gratefully received.

folder_year = str(datetime.today().year) #Save current year as a variable
folder_month = str((datetime.today()- relativedelta(months=1)).strftime("%B")) #Save current month as a variable

yr_directory = "C:\\Users\\Beva\\Documents\\Lettings Index\\"+str(folder_year) #Current year folder link

full_directory = "C:\\Users\\Beva\\Documents\\Lettings Index\\"+str(folder_year)+"\\"+str(folder_month)+"\New Lets Old" #Current month for current year folder link

Upvotes: 0

Views: 1119

Answers (1)

ForceBru
ForceBru

Reputation: 44858

You can use raw strings ("add r") like this:

>>> r'\New Lets Old'
'\\New Lets Old'

Or escape the backslash yourself:

>>> '\\New Lets Old'
'\\New Lets Old'

You're getting the error because Python tries to interpret the \N... part as a Unicode code point:

>>> '\N{LATIN CAPITAL LETTER B}\N{LATIN SMALL LETTER E}\N{LATIN SMALL LETTER V}\N{LATIN CAPITAL LETTER A}'
'BevA'

oops, I couldn't scroll the code in the question to the right originally and didn't see where the \N came from

Upvotes: 1

Related Questions