如何将参数从 subprocess.popen 传递给 powershell 脚本?

时间:2021-04-12 16:08:02

标签: python windows powershell subprocess

我需要在 powershell 脚本中从 python 脚本中获取一个参数。

powershell 代码:

test.ps1
echo $args[0]

我正在尝试使用以下脚本调用 test.ps1

Python 代码:

import subprocess, sys
import os

folder_path=os.getcwd()
p = subprocess.Popen(["powershell.exe",
                      os.path.join(folder_path,"test.ps1"), "-arg1" , 5],
                     stdout=subprocess.PIPE)
res=p.communicate()

执行上述脚本时出现以下错误。

  needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'int' is not iterable

我确定我在语法中遗漏了一些东西,但无法弄清楚。

解决方案:

import subprocess, sys
import os

folder_path=os.getcwd()
p = subprocess.Popen(["powershell.exe",
                      os.path.join(folder_path,"test.ps1"), "-arg1" , "5"],
                     stdout=subprocess.PIPE)
res=p.communicate()

5 应该在双引号中

0 个答案:

没有答案