How to pass an empty argument to Popen (with shell = False)?

I have an external script (sh), I would like to do something like this:

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)

It seems to me that if "arg3" is empty, my script I am called with only two arguments, how can I pass the event "arg3" if it is empty?

+3
source share
3 answers

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))

Running test2.py results

['test.py', 'some string', 'some string2', '']
+4
source
Popen(cmd[0] + [ repr(x) for x in cmd[1:]], 
shell=False, 
stdin=PIPE, 
stdout=PIPE, 
stderr=PIPE)
0
source

Mayby arg3 = '""'?

0
source

Source: https://habr.com/ru/post/1770298/


All Articles