Python calls the Shell method _python

Source: Internet
Author: User

1.1 Os.system (command)

Runs the command command in a child shell and returns the exit state after the command command has finished executing. This is actually implemented using the C standard library function system (). This function needs to reopen a terminal when executing the command command, 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 (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 result of execution of command commands.

1.3 commands.getstatusoutput (command)

Use the OS. The Getstatusoutput () function executes the command command and returns a tuple (Status,output) that represents the return status and execution results of command commands. The execution of the command is actually in accordance with the {command;} 2>&1, so output contains console printout information or error messages. The output does not contain trailing line breaks.

2.1 Subprocess.call (["Some_command", "Some_argument", "Another_argument_or_path"])

Subprocess.call (Command,shell=true)

2.2 subprocess. Popen (command, Shell=true)

Shell=true cannot be saved if the command is not an executable file.
You can use the Subprocess module to create a new process that communicates with the input/output/error pipeline of the new process and gets 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 easiest way to do this 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 child processes.

Assign the result of the calling shell to a python variable

Copy Code code as follows:

Handle = subprocess. Popen (Command, Shell=true, stdout=subprocess.) PIPE)
Print handle.communicate () [0]


In a Python/wxpython environment, there are generally 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 for WX. Exec_async the Wx.excute function returns immediately, if Syn=wx. Exec_sync waits for the calling program to end and then return.

Callback is a WX. Process variable, if callback is not none and syn=wx. Exec_async, the program will call WX when it finishes. Process.onterminate () function.

Os.system () and WX. Execute () uses the system's shell, and the shell window appears when executed. If the console window will pop up under windows, it is not attractive. The following two methods do not have this shortcoming.

3, Class subprocess. Popen

The simplest use is:

Copy Code code as follows:

Import subprocess

Subprocess. Popen (command, Shell=true)

Shell=true cannot be saved if the command is not an executable file.

The previous three methods can only be used to execute programs and open files, cannot handle URLs, and open URL addresses available with WebBrowser modules.

4, Webbrowser.open (URL)

Call the system default browser to open the URL address, such as Webbrowser.open (' http://www.jb51.net '), or you can use
Webbrowser.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 is feasible under Linux.
Above run in Windows2000,python2.4a1,wxpython 2.5.1.
Modify: There is another way: Subprocess.call (*args, **kwargs)

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.