Hi I have read articles related converting backward to forward slashes. But sol was to use raw string.
嗨,我已阅读有关将向后转换为正斜杠的文章。但sol是使用原始字符串。
But Problem in my case is :
但问题在于我的情况是:
I will get file path dynamically to a variable var='C:\dummy_folder\a.txt' In this case i need to convert it to Forward slashes. But due to '\a',i am not able to convert to forward slashes
我将动态获取文件路径变量var ='C:\ dummy_folder \ a.txt'在这种情况下,我需要将其转换为正斜杠。但由于'\ a',我无法转换为正斜杠
How to i convert it? OR How should i change this string to raw string so that i can change it to forward slash
如何转换它?或者我应该如何将此字符串更改为原始字符串,以便我可以将其更改为正斜杠
5 个解决方案
#1
12
Don't do this. Just use os.path and let it handle everything. You should not explicitly set the forward or backward slashes.
不要这样做。只需使用os.path并让它处理所有事情。您不应该显式设置向前或向后斜杠。
>>> var=r'C:\dummy_folder\a.txt'
>>> var.replace('\\', '/')
'C:/dummy_folder/a.txt'
But again, don't. Just use os.path and be happy!
但是,不要。只需使用os.path并开心!
#2
11
There is also os.path.normpath(), which converts backslashes and slashes depending on the local OS. Please see here for detailed usage info. You would use it this way:
还有os.path.normpath(),它根据本地操作系统转换反斜杠和斜杠。有关详细用法信息,请参阅此处。你会这样使用它:
>>> string = r'C:/dummy_folder/a.txt'
>>> os.path.normpath(string)
'C:\dummy_folder\a.txt'
#3
2
Handling paths as a mere string could put you into troubles.; even more if the path you are handling is an user input or may vary in unpredictable ways.
处理路径只是一个字符串可能会让你陷入麻烦。如果您正在处理的路径是用户输入或者可能以不可预测的方式变化,则更多。
Different OS have different way to express the path of a given file, and every modern programming language has own methods to handle paths and file system references. Surely Python and Ruby have it:
不同的OS有不同的方式来表达给定文件的路径,并且每种现代编程语言都有自己的方法来处理路径和文件系统引用。当然Python和Ruby有它:
If you really need to handle strings:
如果你真的需要处理字符串:
- Python: string.replace
- Ruby : string.gsub
Ruby:string.gsub
#4
1
Raw strings are for string literals (written directly in the source file), which doesn't seem to be the case here. In any case, forward slashes are not special characters -- they can be embedded in a regular string without problems. It's backslashes that normally have other meaning in a string, and need to be "escaped" so that they get interpreted as literal backslashes.
原始字符串用于字符串文字(直接写在源文件中),这似乎不是这里的情况。在任何情况下,正斜杠都不是特殊字符 - 它们可以毫无问题地嵌入到常规字符串中。它的反斜杠通常在字符串中具有其他含义,需要“转义”以便将它们解释为文字反斜杠。
To replace backslashes with forward slashes:
用正斜杠替换反斜杠:
# Python:
string = r'C:\dummy_folder\a.txt'
string = string.replace('\\', '/')
# Ruby:
string = 'C:\\dummy_folder\\a.txt'
string = string.gsub('\\', '/')
#5
0
>>> 'C:\\dummy_folder\\a.txt'.replace('\\', '/')
'C:/dummy_folder/a.txt'
In a string literal, you need to escape the \
character.
在字符串文字中,您需要转义\字符。
#1
12
Don't do this. Just use os.path and let it handle everything. You should not explicitly set the forward or backward slashes.
不要这样做。只需使用os.path并让它处理所有事情。您不应该显式设置向前或向后斜杠。
>>> var=r'C:\dummy_folder\a.txt'
>>> var.replace('\\', '/')
'C:/dummy_folder/a.txt'
But again, don't. Just use os.path and be happy!
但是,不要。只需使用os.path并开心!
#2
11
There is also os.path.normpath(), which converts backslashes and slashes depending on the local OS. Please see here for detailed usage info. You would use it this way:
还有os.path.normpath(),它根据本地操作系统转换反斜杠和斜杠。有关详细用法信息,请参阅此处。你会这样使用它:
>>> string = r'C:/dummy_folder/a.txt'
>>> os.path.normpath(string)
'C:\dummy_folder\a.txt'
#3
2
Handling paths as a mere string could put you into troubles.; even more if the path you are handling is an user input or may vary in unpredictable ways.
处理路径只是一个字符串可能会让你陷入麻烦。如果您正在处理的路径是用户输入或者可能以不可预测的方式变化,则更多。
Different OS have different way to express the path of a given file, and every modern programming language has own methods to handle paths and file system references. Surely Python and Ruby have it:
不同的OS有不同的方式来表达给定文件的路径,并且每种现代编程语言都有自己的方法来处理路径和文件系统引用。当然Python和Ruby有它:
If you really need to handle strings:
如果你真的需要处理字符串:
- Python: string.replace
- Ruby : string.gsub
Ruby:string.gsub
#4
1
Raw strings are for string literals (written directly in the source file), which doesn't seem to be the case here. In any case, forward slashes are not special characters -- they can be embedded in a regular string without problems. It's backslashes that normally have other meaning in a string, and need to be "escaped" so that they get interpreted as literal backslashes.
原始字符串用于字符串文字(直接写在源文件中),这似乎不是这里的情况。在任何情况下,正斜杠都不是特殊字符 - 它们可以毫无问题地嵌入到常规字符串中。它的反斜杠通常在字符串中具有其他含义,需要“转义”以便将它们解释为文字反斜杠。
To replace backslashes with forward slashes:
用正斜杠替换反斜杠:
# Python:
string = r'C:\dummy_folder\a.txt'
string = string.replace('\\', '/')
# Ruby:
string = 'C:\\dummy_folder\\a.txt'
string = string.gsub('\\', '/')
#5
0
>>> 'C:\\dummy_folder\\a.txt'.replace('\\', '/')
'C:/dummy_folder/a.txt'
In a string literal, you need to escape the \
character.
在字符串文字中,您需要转义\字符。