【发布时间】:2016-07-02 12:18:56
【问题描述】:
我有一个从包含桌面链接的 .txt 文件创建的字典。我需要将这些链接插入到 powershell 命令中。但是,当我使用 '%s' % my_data[key] 时,我得到了额外的反斜杠,因此 powershell 不会处理该命令,因为它不知道要查找什么,我该如何删除额外的反斜杠?
['powershell.exe', "$sh = New-Object -COM WScript.Shell\n$sh.CreateShortcut('C:\\Users\\johns\\desktop\\python\\Survey+Update\\testing this shared.lnk\n').TargetPath"]
【问题讨论】:
-
你粘贴的那个字典在你的 Python 代码中有一个变量,对吧?
-
是的,因为当我运行它时,它就会出现。完整的命令如下所示。 ([r"powershell.exe", "$sh = New-Object -COM WScript.Shell" + "\n" + "$sh.CreateShortcut('%s').TargetPath" % (my_data[key])] )
-
关于它,给我一分钟
-
你是如何将这个发送到 powershell 的?
-
subprocess.Popen([r"powershell.exe", "$sh = New-Object -COM WScript.Shell" + "\n" + "$sh.CreateShortcut(%s).TargetPath " % my_data[key]], stdout=subprocess.PIPE).communicate()[0]
标签: python python-2.7 powershell dictionary backslash