2

I'm really stuck with a problem I'm hoping someone can help me with. I'm trying to create a wrapper in Python3.1 for a command line program called spooky. I can successfully run this program on the command line like this:

$ spooky -a 4 -b .97

My first Python wrapper attempt for spooky looked like this:

import subprocess

start = "4"
end = ".97"

spooky_path = '/Users/path/to/spooky'
cmd = [spooky_path, '-a', start, '-b', end]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
process.wait()
print('Done')

The above code prints Done, but does not execute the program spooky

Next I tried to just execute the program on the command line like this:

$ /Users/path/to/spooky -a 4 -b .97

The above code also fails, and provides no helpful errors.

My question is: How can I get Python to run this program by sending spooky -a 4 -b .97 to the command line? I would VERY much appreciate any help you can provide. Thanks in advance.

6
  • First you have to investigate why the latter commandline call does not work. Unless you did not fix that problem, you do not have to think about your python approach. What are the "helpfull errors" your command line call produces ? Commented Aug 26, 2011 at 17:52
  • 2
    ...so it runs when you just type in spooky, but doesn't when you give it the full path...obvious reason is that the full path is wrong. Commented Aug 26, 2011 at 17:53
  • Check if that's actually the right path by comparing it to which spooky. Commented Aug 26, 2011 at 17:56
  • On an unrelated note, I think that subprocess.call would be more obvious than subprocess.Popen. Commented Aug 26, 2011 at 17:57
  • @Gerret yes, it runs on the command line when typing either spooky or spooky -a 4 -b .97. If I type the full path it also works, BUT if i type the full path with arguments it DOES NOT work ('/Users/path/to/spooky -a 4 -b .97') Commented Aug 26, 2011 at 18:00

2 Answers 2

5

You need to drop the stdout=subprocess.PIPE. Doing that disconnects the stdout of your process from Python's stdout and makes it retrievable using the Popen.communicate() function, like so:

import subprocess

spooky_path = 'ls'
cmd = [spooky_path, '-l']
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = process.communicate()[0]
print "Output:", output
process.wait()
print('Done')

To make it print directly you can use it without the stdout argument:

process = subprocess.Popen(cmd)

Or you can use the call function:

process = subprocess.call(cmd)
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, this actually works!!! I'm not really sure I understand why your code works considering I was having trouble with the command tool alone? In any case, thank you VERY much for the help.
It was probably working beforehand, you just didn't see the output because it was piped and you weren't displaying it or even attempting to retrieve it.
0

Try making your command into a single string:

cmd = 'spooky_path -a start -b end'
process = subprocess.Popen(cmd, shell=True)

5 Comments

This will just tell you there's no executable "spooky_path -a start -b end".
Unfortunately, once the program is working, I will need to use Python to run this commands repeatedly with different -b values.
That isn't a problem even if it has to be a single string, simply interpolate the desired values into the string, e.g., cmd = "spooky -a %s -b %s" % (a, b) BTW, yes, a single string should work: from the docs: "args should be a string, or a sequence of program arguments. The program to execute is normally the first item in the args sequence or the string if a string is given."
Well, it doesn't for me - Popen("touch this") raises OSError: [Errno 2] No such file or directory: 'touch this', while Popen(["touch", "this"]) works as expected. Python version 3.2 and 2.7.
@delnan: Using a single string works for me if I set shell=True. Answer updated.

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.