Python Literal r'\' Not Accepted

前端 未结 5 2108
感情败类
感情败类 2021-01-17 08:25

r\'\\\' in Python does not work as expected. Instead of returning a string with one character (a backslash) in it, it raises a SyntaxError. r\"\\\"

5条回答
  •  不思量自难忘°
    2021-01-17 08:42

    To address your root problem, you can use / in paths on Windows in Python just fine.

    The r'' and r"" syntax ( raw ) is primarily for working with regular expressions. It doesn't really get you anything in the case of working with paths like you are expecting, especially where the string ends with a \.

    Otherwise if you insist on using \ either use '\\' or "\\", you have to escape the escape character which is \; it isn't pretty, using / or os.path.sep is the best solution.

提交回复
热议问题