10 of the python third-party Library Series-commands library, python-commands
We are talking about using the commands module to execute Linux shell commands. When we use Python to write O & M scripts, we often need to execute linux shell commands, the commands module in Python is used to call the Linux shell Command and return the status and result. The following are the three main functions of the commands module:
1. commands. getoutput ('Shell command ')
2. commands. getstatus ('file ')
3. commands. getstatusoutput ('Shell command ')
Explanations:
1. commands. getoutput ('Shell command ')
Run the shell command to return the result (string type)
import commandscommands.getoutput('pwd')#/Users/admin/PycharmProjects/test
2. commands. getstatus ('file ')
This function has been discarded by python and is not recommended. It returns the ls-ld file Result (String) (the returned result is so strange that it cannot be discarded)
import commandscommands.getstatus('admin.tar')#'-rw-rw-r-- 1 oracle oracle 829440 Jan 29 10:36 admin.tar'
3. commands. getstatusoutput ('Shell command ')
Run the shell command to return the tuple (status, result) of the two elements. status indicates int type, and result indicates string type.
The cmd execution method is {cmd;} 2> & 1. Therefore, the returned results include standard output and standard errors. This is the most commonly used function.
import commandscommands.getstatusoutput('pwd')#(0, '/Users/admin/PycharmProjects/test')