通过powershell命令传递字典项不起作用 [英] Dictionary items not working when passed through powershell command

查看:190
本文介绍了通过powershell命令传递字典项不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从包含桌面链接的.txt文件创建的字典。我需要这些链接才能插入powerhell命令。但是,当我使用'%s'%my_data [key] 我得到额外的反斜杠,因此PowerShell不会处理命令,因为它不知道要查找什么,怎么办我删除额外的反斜杠?

I have a dictionary created from a .txt file that contains desktop links. I need these links to be plugged in to a powershell command. However when I use '%s' % my_data[key] I get extra backslashes and therefore powershell will not process the command because it has no idea what to look for, how do I remove extra backslashes?

['powershell.exe', "$sh = New-Object -COM WScript.Shell\n$sh.CreateShortcut('C:\\Users\\johns\\desktop\\python\\Survey+Update\\testing this shared.lnk\n').TargetPath"]


推荐答案

我发现双反斜杠是不是问题。对于我的代码 subprocess.Popen([rpowershell.exe,r$ sh = New-Object -COM WScript.Shell+\\\
+$ sh.CreateShortcut(%s ).TargetPath%my_data [key]],stdout = subprocess.PIPE).communicate()[0]

I found that the double backslash was not the problem. for my code subprocess.Popen([r"powershell.exe", r"$sh = New-Object -COM WScript.Shell" + "\n" + "$sh.CreateShortcut(%s).TargetPath" % my_data[key]], stdout=subprocess.PIPE).communicate()[0].

\\\
在每一行的末尾。我使用 my_dict [key] .replace(\\\
,)
来摆脱它。还需要路径路径 \%s\修正。我不知道powershell能够处理双反斜杠吗?但是它做了

The dictionary had \n at the end of each line. I used my_dict[key].replace("\n","") to get rid of it. also the path needed "path" \"%s\" fixed that. I don't know how powershell was able to handle the double backslash???? but it did

这篇关于通过powershell命令传递字典项不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆