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

Alex
  • 1,832
  • 3
  • 22
  • 36
  • 2
    Try to add `shell=True` to the `Popen` call. – alecxe Jun 28 '13 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](http://en.wikipedia.org/wiki/Rubber_duck_debugging)) – Paulo Scardine Jun 28 '13 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!!) – Alex Jun 28 '13 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! – alecxe Jun 28 '13 at 19:47

2 Answers2

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

unutbu
  • 711,858
  • 148
  • 1,594
  • 1,547
1

Try to add shell=True to the Popen call.

Also see:

Community
  • 1
  • 1
alecxe
  • 414,977
  • 106
  • 935
  • 1,083