![]() |
VOOZH | about |
The subprocess module is used to run external programs or system commands from Python. It allows to execute commands, capture their output and interact with other processes. One can also send input, handle errors and check execution status.
Example: In this example, a system command is executed to check the Python version and print its output.
Python 3.13.0
Explanation:
subprocess.run() executes a command and waits until it finishes. It returns an object that contains output, errors and return status of the command.
Example: Here, a command is executed and both output and return code are printed.
Output: Python 3.13.0 Return Code: 0
Explanation:
Popen starts a new process and allows interaction with it while it is running. It gives control over input, output and error streams.
Example: In this example, a process is started and its output is captured.
Python 3.13.0
Explanation:
check_output() executes a command and returns only its output. If the command fails, it raises an exception.
Example: Here, a command is executed and its output is directly returned.
Python 3.13.0
Explanation:
subprocess.call() runs a command and returns its exit status. It does not store output in variables.
Example: In this example, a command is executed and its success status is checked.
Python 3.13.0 Success
Explanation:
Pipes are used to send input to a process and receive output from it. This helps in communication between processes.
Example: Here, input is sent to a Python process and the result is captured.
10
Explanation: