通过 powershell 命令传递时字典项不起作用

Dictionary items not working when passed through powershell command

我有一本字典是根据包含桌面链接的 .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"]

尝试这样使用 os.path.normpath:

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

我发现双反斜杠不是问题所在。对于我的代码 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].

字典的每一行末尾都有 \n。我用 my_dict[key].replace("\n","") 来摆脱它。还需要 "path" \"%s\" 修复的路径。我不知道 powershell 如何处理双反斜杠????但确实如此