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
- 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
Popenby providing command arguments (step 3) and redirect
PIPE. (check example below)
- To access completed command execution details call
communicatemethod on process object. (step 4.)
- Check return code of executed command (step 5) using process object's returncode attribute
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 ?
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
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
Popen would print stdout and stderror on process shell which is not very useful.
Note that it is not recommended to access
process.stderr without making sure that command has completed its execution. Use communicate method of process object to overcome this issue.
communicate method of
Popen process object you can make sure that command execution is completed. Communicate returns
stderr of completed command execution. Communicate also updates the process object's
returncode flag with exit code of command execution.
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.