The subprocess
module in Python allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. One of the most commonly used functions in the subprocess
module is run()
. This function runs a command, waits for it to complete, and returns a CompletedProcess
object that holds the output, return code, and other information about the command that was run.
Here's an example of how to use subprocess.run()
to run the command ls -l
and capture its output:
import subprocess
result = subprocess.run(["ls", "-l"], capture_output=True)
print(result.stdout.decode())
In this example, subprocess.run(["ls", "-l"], capture_output=True)
runs the command ls -l
and captures its output. The capture_output
argument is set to True
so that the output of the command is captured and stored in the stdout
attribute of the CompletedProcess
object returned by subprocess.run()
. The stdout
attribute is of type bytes, so we use the decode()
method to convert it to a string.
You can also redirect the error output of a command by using the stderr
attribute of the CompletedProcess
object. Here's an example of how to run the command ls -l /does/not/exist
and capture both its standard output and error output:
import subprocess
result = subprocess.run(["ls", "-l", "/does/not/exist"], capture_output=True)
print(result.stdout.decode())
print(result.stderr.decode())
In this example, subprocess.run(["ls", "-l", "/does/not/exist"], capture_output=True)
runs the command ls -l /does/not/exist
and captures both its standard output and error output. The stdout
attribute holds the standard output and the stderr
attribute holds the error output.
You can also pass input to a command using the input
argument of the subprocess.run()
function. Here's an example of how to run the command grep "hello"
and pass it some input:
import subprocess
result = subprocess.run(["grep", "hello"], input="hello world\nhello python\n", capture_output=True)
print(result.stdout.decode())
In this example, subprocess.run(["grep", "hello"], input="hello world\nhello python\n", capture_output=True)
runs the command grep "hello"
and passes it the input "hello world\nhello python\n". The input
argument is a string that is passed as the input to the command. The stdout
attribute holds the output of the command.
You can also check the return code of the command using the returncode
attribute of the CompletedProcess
object. Here's an example of how to run the command ls -l /does/not/exist
and check its return code:
import subprocess
result = subprocess.run(["ls", "-l", "/does/not/exist"])
if result
Another useful function in the `subprocess` module is `Popen()`, which allows you to start a new process and interact with it while it is running. The `Popen` class takes the same arguments as `run()`, but instead of waiting for the command to complete, it returns a `Popen` object that represents the running process.
Here's an example of how to use `subprocess.Popen()` to run the command `ping localhost` and interact with it:
import subprocess
process = subprocess.Popen(["ping", "localhost"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
output = process.stdout.readline()
if output == b'' and process.poll() is not None:
break
if output:
print(output.decode())
rc = process.poll()
print("Return code: ", rc)
In this example, `subprocess.Popen(["ping", "localhost"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)` starts a new process running the command `ping localhost` and connects its standard output and error output to pipes. The `stdout` attribute of the `Popen` object is used to read the standard output of the command, and the `stderr` attribute is used to read the error output. The while loop reads the output continuously until the process ends and the `poll()` method returns the process return code.
Additionally, to interact with the input of the process, you can use the `stdin` attribute of the `Popen` object. Here's an example of how to use it:
import subprocess
process = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process.stdin.write(b'print("Hello World")\n')
process.stdin.flush()
output = process.stdout.read()
print(output.decode())
This example runs the command `python` and connects its standard input and output to pipes. The `stdin` attribute of the `Popen` object is used to write to the standard input of the command, which in this case is writing the command `print("Hello World")` to the python shell. The `flush()` method is used to ensure the written data is sent to the process. Once the command is executed the output is read and printed.
To summarize, the `subprocess` module in Python provides a number of functions for running and interacting with external commands. The `run()` function is a convenient way to run a command and capture its output and return code, while the `Popen()` function allows you to start a new process and interact with it while it is running. By using the various attributes, such as `stdout`, `stderr`, `stdin` and methods like `poll()`, `read()`, `write()` and `flush()` you can interact and control the process, read and write input and output, and obtain the return code of the command.
## Popular questions
1. How can I capture the output of a command run using `subprocess.run()`?
You can capture the output of a command run using `subprocess.run()` by setting the `capture_output` argument to `True`. For example:
import subprocess
result = subprocess.run(["ls", "-l"], capture_output=True)
print(result.stdout.decode())
2. How can I redirect the error output of a command to a variable?
You can redirect the error output of a command to a variable by using the `stderr` attribute of the `CompletedProcess` object returned by `subprocess.run()`. For example:
import subprocess
result = subprocess.run(["ls", "-l", "/does/not/exist"], capture_output=True)
print(result.stderr.decode())
3. How can I pass input to a command using `subprocess.run()`?
You can pass input to a command using the `input` argument of the `subprocess.run()` function. For example:
import subprocess
result = subprocess.run(["grep", "hello"], input="hello world\nhello python\n", capture_output=True)
print(result.stdout.decode())
4. How can I check the return code of a command run using `subprocess.run()`?
You can check the return code of a command run using `subprocess.run()` by using the `returncode` attribute of the `CompletedProcess` object returned by the function. For example:
import subprocess
result = subprocess.run(["ls", "-l", "/does/not/exist"])
if result.returncode == 0:
print("Command succeeded.")
else:
print("Command failed.")
5. How can I interact with a running process started using `subprocess.Popen()`?
You can interact with a running process started using `subprocess.Popen()` by using the `stdout`, `stderr`, and `stdin` attributes of the `Popen` object. You can read and write to the standard input and output of the process, and also use `poll()` method to check the return code of the process. For example:
import subprocess
process = subprocess.Popen(["ping", "localhost"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
output = process.stdout.readline()
if output == b'' and process.poll() is not None:
break
if output:
print(output.decode())
rc = process.poll()
print("Return code: ", rc)
### Tag
Processing.