While writing python program you may come across situation where there is no python api or library to achieve your task but equivalent shell command exists. In this tutorial we will learn how you can embed shell commands in your python program and access standard output, error and status code generated by command execution.
Steps to create Process and Execute Commands in Python
- Import class
Popen
and flagPIPE
from modulesubprocess
- Store command to be executed in variable as a string.
- Create command arguments list by splitting command string (step 2) using spaces.
- Create new process object of class
Popen
by providing command arguments (step 3) and redirectstdout
andstderr
withPIPE
. (check example below) - To access completed command execution details call
communicate
method on process object. (step 4.) - Check return code of executed command (step 5) using process object's returncode attribute
process.returncode
.
Example
from subprocess import Popen, PIPE
# Command to be executed as a string
command_string = "touch new_file"
# Converts command to be executed as a list
command = command_string.split()
# Spawn Process and Starts Execution
process = Popen(command, stdout=PIPE, stderr=PIPE)
# Returns stdout and stderr after completion of execution
stdout, stderr = process.communicate()
# Exit code of completed command execution
status_code = process.returncode
How does it work ?
Subprocess
Python provides inbuilt module called subprocess
using which you can create (spawn) new operating process from your python application. It's platform independent which means you can create new process in windows, linux or any other operating system.
Popen and PIPE
Subprocess provides class called Popen
for command execution in which you can provide command that you want to execute as list of argument. For example ["ls", "-l"]
.
Popen also allows you to redirect standard output and standard error using PIPE
which means you can store these details inside process object members namely process.stdout
and process.stderr
. Without PIPE
redirection, Popen
would print stdout and stderror on process shell which is not very useful.
Note that it is not recommended to accessprocess.stdout
and process.stderr
without making sure that command has completed its execution. Use communicate method of process object to overcome this issue.
Communicate
Using communicate
method of Popen
process object you can make sure that command execution is completed. Communicate returns stdout
and stderr
of completed command execution. Communicate also updates the process object's returncode
flag with exit code of command execution.
Conclusion
Using subprocess module you can execute any command in any operating system from python script. You can also access stdout and stderror generated by executed command along with exit code.