Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果有';s反斜杠我怎样才能把它改成普通斜杠_Python_Python 3.x - Fatal编程技术网

Python 如果有';s反斜杠我怎样才能把它改成普通斜杠

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

我想在其中编写一个代码,我现在正在从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 char == '\\':
            char.replace(char,'/')

print(address)
我使用操作系统还有其他原因

地址。替换(“\\”,“/”)
它将适用于字符串中的所有“\\”。

address=address.replace(“\\”,“/”)
replace
可与完整字符串一起使用。在代码中,您正在替换名为
char
的不同变量的
'\\'
,而不是变量
地址的
。因此,您没有看到预期的输出。正确的代码应该是地址。替换(“\”、“/”)