3

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?

6
  • how's that supposed to work from command line? Commented Oct 19, 2010 at 11:57
  • sh test.sh 123 345 "" - you can check number of args in script by "echo $#" Commented Oct 19, 2010 at 12:00
  • 1
    so, why your arg3 is not '""' ? Commented Oct 19, 2010 at 12:08
  • Yeah, it works, but is there any better way than checking and escaping any empty string? Commented Oct 19, 2010 at 12:19
  • 1
    @Maciej Kucharz: If passing '""' does work (which is wrong BTW), but '' does not, then your problem is probably in the script you call. If that is a shell script, you may have forgotten to quote the arguments. Besides that, I cannot reproduce your problem. Commented Oct 19, 2010 at 12:39

3 Answers 3

5

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', '']
Sign up to request clarification or add additional context in comments.

Comments

0

Mayby arg3 = '""' ?

Comments

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

1 Comment

repr() works fine, for arguments, but there is a problem with first element which is a path to script

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.