SyntaxError when trying to use backslash for Windows file path

前端 未结 3 1820
灰色年华
灰色年华 2021-01-18 22:22

I tried to confirm if a file exists using the following line of code:

os.path.isfile()

But I noticed if back slash is used by copy&past

3条回答
  •  感情败类
    2021-01-18 22:54

    Because backslashes are escapes in Python. Specifically, you get a Unicode error because the \U escape means "Unicode character here; next 8 characters are a 32-bit hexadecimal codepoint."

    If you use a raw string, which treats backslashes as themselves, it should work:

    os.path.isfile(r"C:\Users\xxx\Desktop\xxx")
    

提交回复
热议问题