Python 如果有';s反斜杠我怎样才能把它改成普通斜杠
我想在其中编写一个代码,我现在正在从python中的用户获取路径,在该路径中,我想将Python 如果有';s反斜杠我怎样才能把它改成普通斜杠,python,python-3.x,Python,Python 3.x,我想在其中编写一个代码,我现在正在从python中的用户获取路径,在该路径中,我想将'\'替换为'/' 我用过re,但它是回溯的告诉我我能做什么 import os,re address = input("Enter Path You of the Folder Which Contains files") check=re.compile('\\') if check.match(address): for char in address: if
'\'
替换为'/'
我用过re,但它是回溯的告诉我我能做什么
import os,re
address = input("Enter Path You of the Folder Which Contains files")
check=re.compile('\\')
if check.match(address):
for char in address:
if char == '\\':
char.replace(char,'/')
print(address)
我使用操作系统还有其他原因地址。替换(“\\”,“/”)
它将适用于字符串中的所有“\\”。
address=address.replace(“\\”,“/”)
replace
可与完整字符串一起使用。在代码中,您正在替换名为char
的不同变量的'\\'
,而不是变量地址的。因此,您没有看到预期的输出。正确的代码应该是地址。替换(“\”、“/”)
。