1. Os.system (Shell_command)
Executes the result directly in the terminal output and returns the execution status 0,1
This function initiates the child process, executes the command in the child process, and returns the exit state after the command executes, if the command has execution, it is displayed in the standard output. This is actually implemented using the C standard library function system ().
Disadvantage: This function needs to reopen a terminal when executing command commands, and cannot save the execution result of command commands .
Os.system ('cat/etc/passwdqc.conf')
2. Os.popen ()
Opens a pipeline between the command process. The return value of this function is a file object that can be read or written (as determined by mode, mode defaults to ' R '). If mode is ' r ', you can use the return value of this function to call read () to get the execution result of the command.
Os.system (cmd) or os.popen (CMD), the former return value is the exit status code of the script, and the return value of the latter is the output of the script during execution. Actual use is subject to demand.
TMP = Os.popen ('ls-l *'). ReadLines ()
3. Commands.getstatusoutput (command)
Use the command function to execute command commands and return a tuple (Status,output) that represents the return status and execution result of the command execution, respectively. Commands.getstatusoutput The command is executed in the same way as the {command;} 2>&1, so the output contains the console outputs or error messages. Output does not contain trailing newline characters.
(status, Output) = Commands.getstatusoutput ("netstat-apn|grep {0} | awk ' {{1}} ' |xargs echo". Format (port,'Print $7')) RR= Re.compile (r'(\d+)/java') op= Rr.findall (Output)#Find all words that contain ' Java ' forIinchRange (Len (OP)): Os.system ("kill-9 {0}". Format (Op[i]))#follow the flag to kill the processOs.system ("Ps-ef | grep {0} | awk ' {{1}} ' | Xargs kill-9". Format (Key,'Print $'))
The actual use of the sense of commands.getstatusoutput (command) is more convenient to use, according to the actual function to be realized using it
Python execution using the shell command method