如何用反斜杠而不是正斜杠书写我的路径?

发布于 2025-01-20 18:11:13 字数 425 浏览 1 评论 0原文

我希望有人可以帮助我,我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不奢求什么 2025-01-27 18:11:13

在 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.

抠脚大汉 2025-01-27 18:11:13

您可以打印出当前的工作目录,而不是写出来:

import os

cwd = str(os.getcwd())
x = cwd.replace("/", "\\")
print(x)

这对我有用,希望对您也有用!

You can print out your current working directory instead of writing it out:

import os

cwd = str(os.getcwd())
x = cwd.replace("/", "\\")
print(x)

This worked for me, hope it does for you as well!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文