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

我有外部脚本(sh),我想做这样的事情:arg1 = some stringarg2 = some string2arg3 = cmd = [/usr/local/bin/myscript, arg1, arg2, arg3]Popen(cmd, shell=False, stdin=PIPE, stdout=PIPE, stderr...

我有外部脚本(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”事件?

解决方法:

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', '']

本文标题为:python-如何将空参数传递给Popen(shell = False)?

基础教程推荐