Search code examples
pythonos.path

Malformed Character escape and python expandvars for user desktop folders


When using os.path.expandvars in Python; I haven't had much of an issue accessing Document folders or AppData folders, but when attempting to to access a Desktop folder the system throws the following error.

Is there another variable that is needed or is the double escape "\ \" really needed for the desktop vs other locations?

if os.path.exists(os.path.join(os.path.expandvars("%userprofile%"),"Desktop"))==True:
print("yes")
               #No issue with this: returns True

if os.path.exists(os.path.join(os.path.expandvars("%userprofile%"),"Desktop\NF"))==True:
                                                                   ^

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 7-8: malformed \N character escape #Need help understanding why an escape is needed for this location ^

if os.path.exists(os.path.join(os.path.expandvars("%userprofile%"),"Documents\Videos"))==True: print("yes") #No issue with this: returns True

The desktop folder NF does exist and existed prior to attempting to access with the code.


Solution

  • Python uses the \N sequence to denote that a unicode character identified by it's name:

    print('\N{LATIN CAPITAL LETTER A}')
    A
    

    Consequently, if a string contains the sequence '\N' which is not starting a '\N{name}' escape, a SyntaxError is raised unless the '\N' is escaped:

    'Deskstop\\New'
    

    or is part of a raw string

    r'Deskstop\New'
    

    or, if the backslash is a path separator, replaced with a forward slash

    'Desktop/New' 
    

    I suspect os.path.join or its pathlib equivalent will handle this situation correctly if used like this:

    os.path.join(os.path.expandvars("%userprofile%"),"Desktop", "New")
    

    but I can't be 100% certain as I'm not on a Windows machine.