如何将空参数传递给Popen(shell = False)?

时间:2010-10-19 11:53:11

标签: python

我有外部脚本(sh),我想做这样的事情:

arg1 = 'some string'
arg2 = 'some string2'
arg3 = ''

cmd = ['/usr/local/bin/myscript', 'arg1', 'arg2', 'arg3']
Popen(cmd, shell=False, stdin=PIPE, stdout=PIPE, stderr=PIPE)

我看来,如果“arg3”为空,我的脚本只用两个参数调用, 如果它是空的,我怎么能传递“arg3”事件?

3 个答案:

答案 0 :(得分:4)

test.py:

import sys
print(sys.argv)

test2.py:

import subprocess
import shlex

cmd="test.py 'some string' 'some string2' '' "
proc=subprocess.Popen(shlex.split(cmd))

运行test2.py产生

['test.py', 'some string', 'some string2', '']

答案 1 :(得分:0)

Popen(cmd[0] + [ repr(x) for x in cmd[1:]], 
shell=False, 
stdin=PIPE, 
stdout=PIPE, 
stderr=PIPE)

答案 2 :(得分:0)

Mayby arg3 = '""'