在字符串中如果有反斜杠如何将其更改为常规斜杠

时间:2021-03-26 12:29:11

标签: 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 char == '\\':
            char.replace(char,'/')

print(address)

我使用 os 是因为其他原因

1 个答案:

答案 0 :(得分:0)

address.replace('\\','/') 它将适用于字符串中存在的所有 '\\'。