Python method for calling shell

Source: Internet
Author: User

1.1 OS. system (command)

Run the command in a sub-shell and return the exit status after the command is executed. This is actually implemented using the C standard library function system. This function needs to re-open a terminal when executing the command, and cannot save the command execution result.

1.2 OS. popen (command, mode)

Open a pipeline between the command process and the command process. The return value of this function is a file object that can be read or written (determined by the mode, the default mode is 'R '). If the mode is 'R', you can use the return value of this function to call read () to obtain the execution result of the command.

1.3 commands. getstatusoutput (command)

Use the OS. getstatusoutput () function to execute the command and return a triple (status, output), indicating the return status and execution result of command execution. The command is actually executed in the {command;} 2> & 1 mode, so the output contains the console output information or error information. Output does not contain line breaks at the end.

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 the command is not an executable file, shell = True cannot be saved.
The subprocess module can be used to create a new process. It can connect to the input, output, and error pipelines of the new process, and obtain the return status of the new process execution. The subprocess module is used to replace old functions or modules such as OS. system (), OS. popen * (), and commands.
The simplest method is to use class subprocess. Popen (command, shell = True ). The Popen class has three useful attributes: Popen. stdin, Popen. stdout, and Popen. stderr, which can communicate with sub-processes.

Assign the shell call result to the python variable.

Copy codeThe Code is as follows:
Handle = subprocess. Popen (command, shell = True, stdout = subprocess. PIPE)
Print handle. communicate () [0]


In a Python/wxPython environment, there are several methods to execute External commands or 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 set to wx. EXEC_ASYNC, the wx. Excute function returns immediately. If the syn is set to wx. EXEC_SYNC, the system returns the result after the called program ends.

Callback is a wx. Process variable. If callback is not set to None and syn = wx. EXEC_ASYNC, the wx. Process. OnTerminate () function is called after the program ends.

Both OS. system () and wx. Execute () use the system shell. A shell window appears during execution. For example, the console window appears in Windows, which is not beautiful. The following two methods do not have this disadvantage.

3. class subprocess. Popen

The simplest usage is:

Copy codeThe Code is as follows:
Import subprocess

Subprocess. Popen (command, shell = True)

If the command is not an executable file, shell = True cannot be saved.

The preceding three methods can only be used to execute programs and open files, but cannot process URLs. You can use the functions provided by the webbrowser module to open URLs.

4. webbrowser. open (url)

You can call the system's default browser to open a URL, such as webbrowser. open ('HTTP: // www.jb51.net ').
Webbrowser. open ('H: \ python.zip ') to run the program. In this way, you do not need to identify the file name or URL, and do not know whether it is feasible in Linux.
The above code is run in Windows2000, Python2.4a1, and wxPython 2.5.1.
Modify: Another method: 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.