In Python, it is easy to run other scripts or programs using the OS module, so that you can use other scripts directly in the script, or the functionality provided by the program, without having to write code that implements the functionality again. To better control the running process, you can use the functions in the Win32process module. If you want to further control the process, you can use the CType module to directly invoke the functions in Kernel32.dll.
1 running other programs with the Os.system function
2 Running other programs with the ShellExecute function
3 Running other programs with the CreateProcess function
4 using cTYPES to invoke functions in kernel32.dll
1 running other programs with the Os.system function
The system () function in the OS module makes it easy to run other programs or scripts. Its function prototype is shown below.
Os.system (command)
The meaning of its parameters is as follows.
command to execute, equivalent to the command entered in the Windows CMD window. If you want to pass parameters to a program or script, you can use a space-delimited program and multiple parameters.
The following example implements a Notepad program that opens the system through the Os.system () function.
>>> Import OS
# Use the Os.system () function to open the Notepad program
>>> os.system (' Notepad ')
0 # return value after Notepad is closed
# Pass parameters to Notepad, open the Python.txt file
>>> Os.system (' Notepad python.txt ')
2 Running other programs with the ShellExecute function
In addition to using the Os.system () 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 meaning of its parameters is as follows.
HWND: The handle of the parent window, or 0 if there is no parent window.
OP: The action to be performed is "open", "print", or empty.
File: The program that you want to run, or the script that opens.
Params: The parameter to pass to the program, or null if it is opened as a file.
Dir: Directory of program initialization.
BShow: Whether the window is displayed.
The following instance uses the ShellExecute function to run other programs.
>>> Import Win32API
# Open Notepad program, run in the background, the window that displays the Notepad program
>>> Win32API. ShellExecute (0, ' open ', ' notepad.exe ', ', ', 0)
# Open Notepad program, run in foreground
>>> Win32API. ShellExecute (0, ' open ', ' notepad.exe ', ', ', 1)
# Pass parameters to Notepad, open python.txt
>>> Win32API. ShellExecute (0, ' open ', ' notepad.exe ', ' python.txt ', ', 1)
# Open the Http://www.python.org Web site in the default browser
>>> Win32API. ShellExecute (0, ' open ', ' http://www.python.org ', ', ', 1)
# play E:\song.wma in the default media Player
>>> Win32API. ShellExecute (0, ' open ', ' e:\\song.wma ', ', ', 1)
# Run the messagebox.py script located in the E:\book\code directory
>>> Win32API. ShellExecute (0, ' open ', ' e:\\book\\code\\messagebox.py ', ', ', 1)
As you can see, using the ShellExecute function is equivalent to double-clicking the file icon in Explorer, and the system opens the appropriate application to perform the action.
3 Running other programs with the CreateProcess function
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 have the following meanings.
AppName: The executable file name.
CommandLine: command-line arguments.
Processattributes: The Process security property, or the default security attribute if none.
Threadattributes: The thread-safe property, or the default security attribute, if none.
bInheritHandles: Inheritance flag.
dwCreationFlags: Create a flag.
Newenvironment: Creates environment variables for the process.
CurrentDirectory: The current directory of the process.
Startupinfo: Creates the properties of the process.
The following instances use Win32process. The CreateProcess function runs the Notepad program.
>>> Import win32process
>>> win32process. CreateProcess (' C:\\windows\\notepad.exe ', ',
None, none, 0, win32process. Create_no_window, none, none,
Win32process. Startupinfo ())
(<? Xml:namespace PREFIX = Pyhandle/>,, 280, 3076)
# function returns the process handle, thread handle, process ID, and thread ID
You can use win32process with a handle to the created process. The TerminateProcess function ends the process, or uses win32event. WaitForSingleObject the thread waiting to be created ends. The function prototypes are as follows.
TerminateProcess (handle, ExitCode)
WaitForSingleObject (handle, milliseconds)
The meanings for the terminateprocess parameters are as follows.
Handle: The handle of the process to manipulate.
ExitCode: Process exit code.
The meanings for the WaitForSingleObject parameters are as follows.
Handle: The handle of the process to manipulate.
Milliseconds: Wait time, if 1, then wait.
The following instance implements and operates after the process is created.
>>> Import win32process
# Open the Notepad program and get its handle
>>> handle = win32process. CreateProcess (' C:\\windows\\notepad.exe ',
", none, none, 0, win32process. Create_no_window, none, none,
Win32process. Startupinfo ())
# Use the TerminateProcess function to terminate the Notepad program
>>> win32process. TerminateProcess (handle[0],0)
# import Win32event Module
>>> Import Win32event
# Create process get handle
>>> handle = win32process. CreateProcess (' C:\\windows\\notepad.exe ',
", none, none, 0, win32process. Create_no_window, none, none,
Win32process. Startupinfo ())
# Wait for the process to end
>>> win32event. WaitForSingleObject (Handle[0],-1)
0 # return value of Process end
4 using cTYPES to invoke functions in kernel32.dll
Using the cTYPES module enables Python to invoke functions that reside in the dynamic-link library. The cTYPES module is already included in Python version 2.5. If you use a different version of Python, you can download the installation to the Http://python.net/crew/theller/ctypes website. cTYPES applies to Python version 2.3 and above.
1. cTYPES Introduction
cTYPES provides Python with the ability to invoke functions in a dynamic-link library. Using cTYPES, you can easily invoke a dynamic-link library written in C and pass parameters to it. cTYPES defines the basic data types in the C language, and can implement structs and unions in C. cTYPES can work on platforms such as Windows, Windows CE, Mac OS X, Linux, Solaris, FreeBSD, OpenBSD, and so on, to achieve a virtually cross-platform.
The following example uses cTYPES to implement a MessageBoxA function that calls user32.dll directly under Windows. Run as shown in post 10-6.
>>> from ctypes Import *
>>> User32 = Windll. LoadLibrary (' user32.dll ') # Load dynamic link library
>>> User32. MessageBoxA (0, ' ctypes is cool! ', ' cTYPES ', 0)
# Call the MessageBoxA function.
Transferred from: http://book.51cto.com/art/200710/58183.htm
"Go" python Call (run) external program