如何在 python 中使用反斜杠和变量创建字符串?
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加“\\”以在所需位置创建反斜杠:
Add a "\\" to create a backslash in the desired place: