如何用反斜杠而不是正斜杠书写我的路径?
我希望有人可以帮助我,我在Google上任何地方都找不到这个问题的答案。
我需要用后斜线路径替换前向斜线路径,以便与Windows命令提示符起来。前向斜线路径适用于本地文件夹,即c:/users/lorcan-但不是网络文件夹,即// NetworkFolder/Storage,
我了解到您不能使用Python中的Backsslash,因为这是一个特殊的角色,因此您必须使用两个后斜线。但是,这会导致我的路径太多,而命令提示符不起作用。
>>> s = '//Networkfolder/Storage/Myfolder/Myfile'
>>> s2 = s.replace('/','\\')
>>> s2
'\\\\Networkfolder\\Storage\\Myfolder\\Myfile'
I hope someone can help as I am stuck, I can't find the answer to this problem anywhere on google.
I need to replace my forward slash path with a backslash path in order for it to work with windows command prompt. Forward slash paths work for local folders, i.e. C:/Users/Lorcan - but not network folders, i.e. //Networkfolder/Storage
I learned that you can't use the backslash in python as it is a special character, so you must use two backslashes. However, this causes my path to have too many backslashes, and command prompt doesn't work.
>>> s = '//Networkfolder/Storage/Myfolder/Myfile'
>>> s2 = s.replace('/','\\')
>>> s2
'\\\\Networkfolder\\Storage\\Myfolder\\Myfile'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 python shell 中,反斜杠显示为
\\
,但在字符串中实际上是\
。您的代码工作正常,真正的字符串是正确的,它是这样显示的。In the python shell, backslashes are displayed as
\\
, but it's really\
in the string. Your code is working fine, the real string is correct, it's being displayed like that.您可以打印出当前的工作目录,而不是写出来:
这对我有用,希望对您也有用!
You can print out your current working directory instead of writing it out:
This worked for me, hope it does for you as well!