Maciej Kucharz
Maciej Kucharz

Reputation: 1443

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

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

I seems, that if "arg3" is empty, my script i called only with two arguments, how can I pass "arg3" event if it's empty?

Upvotes: 3

Views: 2815

Answers (3)

Paulo Scardine
Paulo Scardine

Reputation: 77251

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

Upvotes: 0

Tony Veijalainen
Tony Veijalainen

Reputation: 5555

Mayby arg3 = '""' ?

Upvotes: 0

unutbu
unutbu

Reputation: 879471

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 yields

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

Upvotes: 5

Related Questions