Python calls shell command three ways

Source: Internet
Author: User
Tags call shell

#!/usr/bin/python is to tell the operating system to execute the script, call/usr/bin under the Python interpreter;
#!/usr/bin/env python is used to prevent operating system users from not loading Python into the default/usr/bin path.

There are many ways that Python invokes shell commands1.1 os.system (command)runs command commands in a child shell and returns the exit status after command execution is complete. This is actually implemented using the C standard library function system (). This function will need to reopen a terminal when executing command commands, and cannot save the execution result of command commands. 1.2 Os.popen (Command,mode)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. 1.3 commands.getstatusoutput (command)Use the Os.popen () function to execute a command command and return a tuple (Status,output) that represents the return status and execution result of the command execution. 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.  2.1 Subprocess.call (["Some_command", "Some_argument", "Another_argument_or_path"])Subprocess.call (command,shell=true)2.2 subprocess. Popen (Command, shell=true)if command is not an executable file, Shell=true cannot be saved. using the Subprocess module, you can create new processes that can connect to the input/output/error pipelines of the new process and get the return status of the new process execution. The purpose of using the Subprocess module is to replace old functions or modules such as Os.system (), os.popen* (), commands.*, and so on. The simplest way is to use class subprocess. Popen (command,shell=true). The Popen class has popen.stdin,popen.stdout,popen.stderr three useful properties that enable communication with the child process. Assign the result of the call shell to a Python variablehandle = subprocess. Popen (Command, Shell=true, stdout=subprocess. PIPE)print handle.communicate () [0]
2.3with Commands module. In fact, it is also the encapsulation of Popen . This module mainly has the following methods
commands. Getstatusoutput ( cmd) returns (status, output).
commands. GetOutput ( cmd) returns only output results
commands. GetStatus ( file) returns the execution result string of the LS-ld file, called the GetOutput, and is not recommended for this method .
Such as
>>>ImportCommands>>>Commands.Getstatusoutput(' Ls/bin/ls ')(0, '/bin/ls ')>>>Commands.getstatusoutput ( ' cat/bin/junk '  ("Cat:/bin/junk:no such file or directory ') >>>  Commands. Getstatusoutput ( '/bin/junk ' )  (p, ' SH:/bin/ Junk:not found ') >>> commands. Getoutput ( ' ls/bin/ls ' )  '/bin/ls ' >>> commands. Getstatus ( '/bin/ls ' )  '-rwxr-xr-x 1 root 13352 Oct 1994/bin/ls '              
in a Python/wxpython environment, there are typically several ways to execute an external command or to start another program in a python program:1. Os.system (command)2, WX. Execute (command, SYN=WX. Exec_async, Callback=none)if the SYN is WX. Exec_async the Wx.excute function immediately returns, if Syn=wx. Exec_sync waits for the calling program to end before returning. callback is a WX. Process variable, if callback is not none and syn=wx. Exec_async, the WX will be called after the program finishes. The Process.onterminate () function. Os.system () and WX. Execute () takes advantage of the system's shell and executes with the shell window. If the console window pops up under Windows, it's not beautiful. The following two methods do not have this disadvantage. 3, Class subprocess. PopenThe simplest usage is:Import subprocesssubprocess. Popen (Command, shell=true)if command is not an executable file, Shell=true cannot be saved. the previous three methods can only be used to execute programs and open files, cannot handle URLs, open URL addresses can be provided by the WebBrowser module functionality. 4. Webbrowser.open (URL)Call the system default browser to open the URL address, such as Webbrowser.open (' http://www.google.com '), or you can usewebbrowser.open (' H:\python.zip ') to execute the program. This eliminates the need to distinguish between a file name or a URL, and does not know if it works under Linux. above run in Windows2000,python2.4a1,wxpython 2.5.1. Modify: There is another way: Subprocess.call (*args, **kwargs) 

Python calls shell command three ways

Related Article

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.