Python execution using the shell command method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.