Python Pass变量到subprocess.Popen

时间:2018-01-26 20:06:47

标签: python subprocess

我有一个python脚本(gcin.py),它给我一个遥控器的状态,0 =向下1 =向上。

/usr/local/bin/gcin.py -l 2010
0

我试图使用子进程在另一个python脚本(GetCut.py)中运行此脚本,但无法识别sys.argv [1]变量。

import subprocess,sys
lId=int(sys.argv[1])
p = subprocess.Popen("python /usr/local/bin/gcin.py","-l", lId, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()

if output == 1:
    print "In"
else:
    print "Out"

python GetCut.py 2010

给我这个错误:

Traceback (most recent call last):
  File "GetCut.py", line 4, in <module>
    p = subprocess.Popen("python /usr/local/bin/gcin.py","-l", lId, stdout=subprocess.PIPE, shell=True)
  File "/usr/lib64/python2.7/subprocess.py", line 660, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

我很感激任何帮助。

1 个答案:

答案 0 :(得分:0)

您需要使用列表将参数传递给子流程。您可能也不想使用shell=True

subprocess.Popen([sys.executable, "/usr/local/bin/gcin.py", "-l", str(lId)], stdout=subprocess.PIPE)

如果您只想要程序的输出,可以使用check_output

subprocess.check_output([sys.executable, "/usr/local/bin/gcin.py", "-l", str(lId)])