1

I have a c-executable that works as:

./avg filename opt1 opt2 opt3 

Eg:

./avg one.dat 1 1 -1

returns something like

 127.504            2.244375804433813           1.111195552742103 blks: 500

When I try to call this from a python code, I have tried:

import os
os.system("./avg one.dat 1 1 -1")

as well as

import subprocess
#args = ("./avg","one.dat","1","1","-1")
args = ("./avg  one.dat  1  1  -1")
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
print output

Neither works, and the executable that not enough arguments are passed to the executable. It should get at least 4 options.

Any help will be appreciated, thanks in advance.

7
  • In what way doesn't it work? Commented Mar 7, 2016 at 14:54
  • @PeterWood “the executable [complains] that not enough arguments are passed to the executable” Commented Mar 7, 2016 at 14:54
  • Once the application is compiled, the fact it is written in c is meaningless. Removing the tag. Commented Mar 7, 2016 at 14:55
  • Maybe it's a problem with the executable. Commented Mar 7, 2016 at 14:56
  • make your args a tuple ("./avg","one.dat","1","1","-1") . Popen wants the arguments as separate elements in the iterable, not space delineated in a string Commented Mar 7, 2016 at 14:56

1 Answer 1

1

You cannot wait() for the subprocess before you read its output; wait waits for the child process to terminate, but child process is blocked writing its output! subprocess.check_output simplifies reading stdout of a process, so you can replace Popen, wait and read with

output = subprocess.check_output(['./avg', 'one.dat', '1', '1', '-1']) 

And args = ("./avg","one.dat","1","1","-1") (or a list as above) is the proper way to do it; each argument need to be separate elements in an iterable.

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

1 Comment

You can also use shlex.split to convert './avg one.dat 1 1 -1' to ['./avg', 'one.dat', '1', '1', '-1']

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.