0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

It's the \N in "\New Lets Old" string, not the \U
@VPfB, right, I think my browser lagged, and I was unable to scroll to the right to see that, so I thought the \N in the error message was a typo or something

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.