1 Use functions to run other programs
2 Use the ShellExecute function to run other programs
3 Use the CreateProcess function to run other programs
4 Calling functions using ctypes
1 Use functions to run other programs
The system() function in the os module can easily run other programs or scripts. Its function prototype is shown below.
(command)
The parameters are as follows.
The command to be executed is equivalent to the command entered in the cmd window of Windows. If you want to pass parameters to a program or script, you can use spaces to separate the program and multiple parameters.
The following example implements the Notepad program that opens the system through the () function.
|
2 Use the ShellExecute function to run other programs
In addition to using the() function in the os module, you can also use the ShellExecute() function in the win32api module. Its function is shown below.
ShellExecute(hwnd, op , file , params , dir , bShow )
The parameters are as follows.
hwnd: The handle to the parent window, if there is no parent window, it is 0.
op: The operation to be performed is "open", "print", or empty.
file: The program to run, or the script to open.
params: The parameter to be passed to the program, if the opened file, it is empty.
dir: the directory in which the program is initialized.
bShow: Whether to display the window.
The following example uses the ShellExecute function to run other programs.
>>> import win32api
# Open the Notepad program and run it in the background, which means the window of the Notepad program is displayed.
>>> (0, 'open', '', '','',0)
# Open Notepad program and run it in the foreground
>>> (0, 'open', '', '','',1)
# Pass parameters to Notepad and open
>>> (0, 'open', '', '','',1)
# Open in the default browserwebsite
>>> (0, 'open', '', '','',1)
# Play E:\ in the default media player
>>> (0, 'open', 'E:\\', '','',1)
# Run the script located in the E:\book\code directory
>>> (0, 'open', 'E:\\book\\code\\', '','',1)
It can be seen that using the ShellExecute function is equivalent to double-clicking the file icon in the explorer, and the system will open the corresponding application to perform operations.
3 Use the CreateProcess function to run other programs
In order to facilitate control of programs running through scripts, you can use the CreateProcess() function in the win32process module. Its function prototype is shown below.
CreateProcess(appName, commandLine , processAttributes , threadAttributes , bInheritHandles , dwCreationFlags , newEnvironment , currentDirectory , startupinfo ) |
The parameters are as follows.
appName: executable file name.
commandLine: command line argument.
processAttributes: Process security attribute, if None, the default security attribute.
threadAttributes: thread-safe attribute, if None, the default security attribute.
bInheritHandles: Inherit flag.
dwCreationFlags: Create flags.
newEnvironment: Creates the environment variable of the process.
currentDirectory: The current directory of the process.
startupinfo: Create a property of the process.
The following example runs the Notepad program using a function.
|
With the handle to the created process, you can end the process with a function, or use the thread waiting for the creation to end. The function prototypes are as follows.
TerminateProcess(handle, exitCode)
WaitForSingleObject(handle, milliseconds )
The meanings of TerminateProcess parameters are as follows.
handle: The process handle to operate.
exitCode: process exit code.
The meanings of WaitForSingleObject parameters are as follows.
handle: The process handle to operate.
millionseconds: The waiting time, if it is ?1, it will wait.
The following example implements and operates on the process.
|
4 Calling functions using ctypes
Use the ctypes module to make Python call functions located in dynamic link libraries. The ctypes module is already included in Python version 2.5. If you use another version of Python, you can go to/crew/theller/ctypesDownload and install the website. ctypes are suitable for Python version 2.3 and above.
1. Introduction to ctypes
ctypes provides Python with the function of calling functions in dynamic link libraries. Use ctypes to easily call dynamic link libraries written in C and pass parameters to them. ctypes defines the basic data types in C language and can implement structures and unions in C language. ctypes can work on Windows, Windows CE, Mac OS X, Linux, Solaris, FreeBSD, OpenBSD and other platforms, basically implementing cross-platform.
The following example uses ctypes to implement the MessageBoxA function directly called under Windows. After running, it is shown in Figure 10-6.
|
1
498)=498;" border=0> |
Figure 10-6 Using ctypes
2. Data type and structure
。。。
3. Change program flow in use functions
。。。
The above comes from——Conquer Python—Language Basics and Typical Applications
See:http://book./art/200710/