3

I have some python code, from which I want to call another program. This program will

  • Print some output to STDOUT
  • Write a file to disk

Using call I get the following behavior;

from subprocess import call
call(['./tango_x86_64_release', 'VTS1 ct="N" nt="N" ph="7.2" te="303" io="0.02" seq="MKHPYEEFPTGSKSPYNMSRGAHPGAV"'])
34, File not properly written, try writing it up again, 
1

This happens regardless if if the arguments are split into a list or not;

call(['./tango_x86_64_release', 'VTS1', 'ct="N"', 'nt="N"', 'ph="7.2"', 'te="303"', 'io="0.02"', 'seq="MKHPYEEFPTGSKSPYNMSRGAHPGAV"'])
34, File not properly written, try writing it up again, 
1

I can call this same command from the my terminal

./tango_x86_64_release VTS1 ct="N" nt="N" ph="7.2" te="303" io="0.02" seq="MKHPYEEFPTGSKSPYNMSRGAHPGAV"

Which works and gives an exit status of 0.

It seems like its the writing to disk which is causing issues, if I break the command then I get the appropriate warning message (i.e. remove an argument, it warns me that the argument is missing).

Using subprocess.Popen() gives an OSError;

import subprocess as sub
output = sub.Popen('./tango_x86_64_release VTS1 ct="N" nt="N" ph="7.2" te="303" io="0.02" seq="MKHPYEEFPTGSKSPYNMSRGAHPGAV"', stdout=sub.PIPE, stderr=sub.PIPE)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.7/subprocess.py", line 1249, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

Any help greatly appreciated

4
  • 2
    Try to add shell=True to the Popen call. Commented Jun 28, 2013 at 19:41
  • 1
    Seems hard to reproduce outside your environment. Trying to create a small, reproducible use case probably will give you the answer (kind of rubber-duck debugging) Commented Jun 28, 2013 at 19:43
  • @alecxe - you're my hero. Bang on the money (post it as an answer - I can't believe that's all it took!!) Commented Jun 28, 2013 at 19:44
  • @Alex no problems. All I did is just found two relevant threads that I've provided below - people there are your heroes! Commented Jun 28, 2013 at 19:47

2 Answers 2

3

Use shlex.split to split the command for you:

import shlex
call(shlex.split('./tango_x86_64_release VTS1 ct="N" nt="N" ph="7.2" te="303" io="0.02" seq="MKHPYEEFPTGSKSPYNMSRGAHPGAV"'))

Note that although you might be able to solve your problem by adding shell=True, you should avoid it if possible, since it can be a security risk (search for "shell injection").

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

Comments

1

Try to add shell=True to the Popen call.

Also see:

Comments

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.