How Python calls the shell

Source: Internet
Author: User
Tags call shell
1.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. The Getstatusoutput () function executes the command command and returns 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 variable

Copy the Code code as follows:


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


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. Popen

The simplest usage is:

Copy the Code code as follows:


Import subprocess

Subprocess. 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.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 works 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.