Python:将反斜杠替换为斜杠

Python:将反斜杠替换为斜杠,python,syntax-error,special-characters,Python,Syntax Error,Special Characters,我有一个带有反斜杠的路径,比如path=F:\Downloads\Images\Product\Samples,但我想用斜杠替换反斜杠。 我尝试了path.replace\,/或product_image.translate{ordc:/for c in\},但在扫描字符串文字时得到了SyntaxError:EOL。怎么了 path = "F:\Downloads\Images\Product\Samples" path.replace("\\",r'/'

我有一个带有反斜杠的路径,比如path=F:\Downloads\Images\Product\Samples,但我想用斜杠替换反斜杠。 我尝试了path.replace\,/或product_image.translate{ordc:/for c in\},但在扫描字符串文字时得到了SyntaxError:EOL。怎么了

path = "F:\Downloads\Images\Product\Samples"
path.replace("\\",r'/')

这样就可以了。

您必须通过\来转义特殊字符。尝试:

path.replace("\\","\/")
现在找到这个,它是重复的