Python calls methods of the shell

  • 2020-04-02 13:11:55
  • OfStack

1.1   OS. The system (command)

Run the command command in a subshell and return the exit status after the command is executed. This is actually implemented using the C standard library function system(). This function reopens a terminal when executing a command and cannot save the result of the command.

1.2   OS. Popen (command, mode)

Open a pipe to the command process. The return value of this function is a file object that can be read or written (as determined by mode, which defaults to 'r'). If the mode is 'r', you can use the return value of this function to call read() to get the result of the command.

1.3   Commands. Getstatusoutput (command)

The command command is executed using the os.getstatusoutput () function and a tuple (status,output) is returned, representing the return status and the result of the command execution, respectively. Command is actually executed by {command; } 2 > So the output contains console output information or error information. The output does not contain a trailing line break.

2.1   Subprocess. Call ([" some_command ", "some_argument", "another_argument_or_path"])

Subprocess. The call (the command, the shell = True)

2.2   Subprocess. Popen (command, shell = True)

If command is not an executable, shell=True is not obtainable.
Using the subprocess module, you can create a new process, connect to the input/output/error pipeline of the new process, and get the return status of the execution of the new process. The purpose of using the subprocess module is to replace older functions or modules such as os.system(), os.popen*(), commands.*, etc.
The easiest way is to use class subprocess.popen (command,shell=True). The Popen class has three useful properties: popen.stdin, popen.stdout, and popen.stderr, which enable communication with child processes.

Assigns the result of calling the shell to a python variable


handle = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
print handle.communicate()[0]


In the Python/wxPython environment, there are usually several ways to execute an external command or start another program in a Python program:

1, OS. System (command)

2. Wx.execute (command, syn= wx.exec_async, callback=None)

If syn is set to wx.exec_async, the wx.excute function returns immediately; if syn= wx.exec_sync, it waits until the called program is finished before returning.

The callback is a wx.process variable, and if the callback is not None and syn= wx.exec_async, the wx.process.onterminate () function is called when the program is finished.

Both os.system() and wx.execute () make use of the system's shell, and a shell window appears when executed. If under Windows will pop up the console window, not beautiful. The following two methods do not have this disadvantage.

3, class subprocess. Popen

The simplest use is:


import subprocess
subprocess.Popen(command, shell=True)

If command is not an executable, shell=True is not obtainable.

The first three methods can only be used to execute programs and open files, not urls, which can be opened using the functionality provided by the webbrowser module.

4, webbrowser. Open (url)

Calling the system's default browser to open the URL address, such as webbrowser.open('//www.jb51.net'), can also be used
Webbrowser.open ('h:\python.zip') to execute the program. This way you don't have to tell the filename from the URL, and you don't know if it works under Linux.
This runs on Windows2000, python 2.4a1, and wxPython 2.5.1.
Modify: subprocess.call(*args, **kwargs)


Related articles: