如何在 python 中使用反斜杠和变量创建字符串?

发布于 2025-01-10 17:02:36 字数 690 浏览 0 评论 0原文

我正在尝试在 Windows 计算机上执行此命令:

dir \\hostname\sapmnt\SID\SYS\profile\SID_*04_hostname /b /a-d

到目前为止,我有以下代码:

cmd_drive = r"\\"
local_hostname = "hostname"
current_sid = "SID"
currentline_instance_number = "04"
cmd_pf = os.path.join(cmd_drive, local_hostname, "sapmnt", current_sid, "SYS", "profile")
cmd_pf = cmd_pf + str(current_sid) + "_*" + str(currentline_instance_number) + "_" + str(currentline_host)
cmd_pf = "dir " + cmd_pf + " /b /a-d"
print(cmd_pf)

产生以下输出:

dir \\hostname\sapmnt\SID\SYS\profileSID_*04_hostname /b /a-d

因此,我需要在字符串的最后部分之前有一个反斜杠 ("SID_*04_hostname /b /广告”)

I'm trying to execute this command on a Windows machine:

dir \\hostname\sapmnt\SID\SYS\profile\SID_*04_hostname /b /a-d

So far, I have this code:

cmd_drive = r"\\"
local_hostname = "hostname"
current_sid = "SID"
currentline_instance_number = "04"
cmd_pf = os.path.join(cmd_drive, local_hostname, "sapmnt", current_sid, "SYS", "profile")
cmd_pf = cmd_pf + str(current_sid) + "_*" + str(currentline_instance_number) + "_" + str(currentline_host)
cmd_pf = "dir " + cmd_pf + " /b /a-d"
print(cmd_pf)

which produces this output:

dir \\hostname\sapmnt\SID\SYS\profileSID_*04_hostname /b /a-d

So, I need a backslash before the final part of the string ("SID_*04_hostname /b /a-d")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不气馁 2025-01-17 17:02:36

添加“\\”以在所需位置创建反斜杠:

cmd_drive = r"\\"
    local_hostname = "hostname"
    current_sid = "SID"
    currentline_instance_number = "04"
    cmd_pf = os.path.join(cmd_drive, local_hostname, "sapmnt", current_sid, "SYS", "profile")
    cmd_pf = cmd_pf + "\\" + str(current_sid) + "_*" + str(currentline_instance_number) + "_" + str(currentline_host)
    cmd_pf = "dir " + cmd_pf + " /b /a-d"
    print(cmd_pf)`

Add a "\\" to create a backslash in the desired place:

cmd_drive = r"\\"
    local_hostname = "hostname"
    current_sid = "SID"
    currentline_instance_number = "04"
    cmd_pf = os.path.join(cmd_drive, local_hostname, "sapmnt", current_sid, "SYS", "profile")
    cmd_pf = cmd_pf + "\\" + str(current_sid) + "_*" + str(currentline_instance_number) + "_" + str(currentline_host)
    cmd_pf = "dir " + cmd_pf + " /b /a-d"
    print(cmd_pf)`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文