4

I have spent multiple hours trying to figure out how to pass multiple parameters to python script which is supposed to be executed by subprocess.Popen without any luck.

Script:

command = ['/usr/bin/python', '/tmp/script.py mcl=NULL mtp=data mnm=DS4INST \
          mno=NULL mse=NULL mce=cll01'] 

result = subprocess.Popen(command, stdout = subprocess.PIPE, \
         stderr = subprocess.PIPE)

out, err = result.communicate()

print out, err 

I receive following error message:

python: can't open file '/tmp/script.py mcl=NULL mtp=data mnm=DS4INST mno=NULL \
mse=NULL mce=cll01': [Errno 2] No such file or directory 

However, when I execute script directly from shell

/usr/bin/python /tmp/script.py mcl=NULL mtp=data mnm=DS4INST mno=NULL \
mse=NULL mce=cll01

I receive desired output and error message isn't generated.

Please advise.

1 Answer 1

6

Try this:

command = ['/usr/bin/python', '/tmp/script.py', 'mcl=NULL', 'mtp=data', 'mnm=DS4INST', 'mno=NULL' 'mse=NULL', 'mce=cll01'] 

In your code, the second element of command is considered as one single argument and interpreted as:

/usr/bin/python "/tmp/script.py mcl=NULL mtp=data mnm=DS4INST mno=NULL mse=NULL mce=cll01"

so just like a long filename with spaces.

You have to split the arguments into separate elements of the command list.

Sign up to request clarification or add additional context in comments.

3 Comments

this one, for some reason executes script.py with each parameter separately
@m1k3y02 - I would then try a compromise: ['/usr/bin/python', '/tmp/script.py', 'mcl=NULL mtp=data …']
it works, but sys.argv recognizes all parameters as one, so there is additional parsing required which I can handle. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.