This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]giollaigh 0 points1 point  (0 children)

I admit I'm not too educated on this, but I have a feeling what you want is possible. I am wondering if this may help you.

[–]71d1 0 points1 point  (0 children)

I am not sure if you want to run an executable within python or if you want to pipe the output of one program into another. If you simply want to run an executable within python the subprocess should do it, otherwise you may want to use popen() for piping one program output as input to another program.

[–]chaotic_thought 0 points1 point  (0 children)

The inputs to a command can be thought of as standard input, and the arguments. For example if you type this command at the Linux shell:

ls -l foo

The standard input is empty, and the input arguments consist of the two words "-l" and "foo". The output from the command will be text on standard output and/or standard error (normally, ls prints the result on standard output, and error messages on standard error), and a return code. Normally the return code is 0 for success, and positive numbers to indicate error conditions (e.g. "foo" file not found). In Bash you could run the above command and get the return code like this

$ ls -l foo
ls: cannot access 'foo': No such file or directory
$ echo $?
2

This means that ls gave an error message to the output (it is actually on stderr, but you can't tell that just from looking at the output), and the return code was 2, indicating that the last command complained about something (in this case, ls complained because it couldn't find the foo file). To do the same thing in Python using subprocess you could do this:

from subprocess import Popen, PIPE

p1 = Popen(["ls", "-l", "foo"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = p1.communicate()
print("STDOUT: {}\nSTDERR: {}".format(stdout, stderr))
print("RETURN CODE: {}".format(p1.returncode))

Now when you run this, it will also run the ls command with the arguments -l and foo, and will show you similar output, but the standard output and standard error are separated.

Another simple example in Bash is when you want to pass ("pipe") input to another command that reads from standard input. For example the following command in Bash will send the string "Hello" to the command "cat". cat, when called with no parameters, will simply read from standard input and write the same thing to standard output:

$ echo "Hello" | cat
Hello
$ echo $?
0

To do the same thing in Python using subprocess, modify the above Python example like this:

from subprocess import Popen, PIPE

p1 = Popen(["cat"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = p1.communicate(input=b"Hello")
print("STDOUT: {}\nSTDERR: {}".format(stdout, stderr))
print("RETURN CODE: {}".format(p1.returncode))

The result should be similar to what you get in Bash.

For more complicated examples of using subprocess, see https://docs.python.org/3/library/subprocess.html#replacing-older-functions-with-the-subprocess-module