In Python, you can easily use the os module to run other scripts or programs, so that you can directly use the functions provided by other scripts or programs in the script without having to write the implementation again. The code for this function. In order 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 call the functions in kernel32.dll. The following introduces 4 methods:
1. os.system() function
The system() function in the os module can easily run other programs or scripts. The mode is as follows:
os.system(command): command: command to be executed. If you want to pass parameters to the script, you can use spaces to separate the program and multiple parameters.
Example:
#打开记事本 os.system('notepad') #用记事本打开aa.txt os.system('notepad aa.txt') #aa.txt文件必须在当前程序目录 #直接打开aa.txt os.system('aa.txt') #直接打开Excel文件 os.system('aa.xlsx') #直接打开Word文件 os.system('bb.docx') filepath='测试.xlsx' #打开包含中文的文件 os.system(filepath.decode('utf8').encode('GBK'))
2, ShellExecute Function
Use the ShellExecute() function in the win32api module to run other programs, the format is as follows
ShellExecute(hwnd, op, file, args, dir, show)
hwnd: The handle of the parent window, if there is no parent window, it is 0
op: The operation to be run, which is open, print or empty
file: The program to be run, Or the opened script
args: Parameters to be passed to the program, empty if the file is opened
dir: The directory initialized by the program
show: Whether to display the window
Using the ShellExecute function is equivalent to double-clicking the file icon in the resource manager, and the system will open the corresponding program to run.
import win32api win32api.ShellExecute(0, 'open', 'notepad.exe', '', '', 0) # 后台执行 win32api.ShellExecute(0, 'open', 'notepad.exe', '', '', 1) # 前台打开 win32api.ShellExecute(0, 'open', 'notepad.exe', 'wmi.txt', '', 1) # 打开文件 win32api.ShellExecute(0, 'open', 'iexplore.exe', '', '', 1) # 打开IE浏览器 win32api.ShellExecute(0, 'open', 'iexplore.exe', 'https://www.baidu.com/', '', 1) # 用IE浏览器打开百度网址 win32api.ShellExecute(0, 'open', 'mspaint.exe', 'wxqr.png', '', 1) #用系统附件自带的画图打开图片wxqr.png
3. CreateProcess
Create process:
In order to facilitate the control of programs run through scripts, you can use the CreateProcess() function in the win32process module to create a process that runs the corresponding program. process. Its function format is: CreateProcess(appName, cmdLine, proAttr, threadAttr, InheritHandle, CreationFlags, newEnv, currentDir, Attr)
appName:executable file name
cmdLine:command line parameters
procAttr : Process safety attribute
threadAttr: Thread safety attribute
InheritHandle: Inheritance flag
CreationFlags: Creation flag
currentDir: The current directory of the process
Attr: Attributes of the created program
End the process:
You can use the win32process.TerminateProcess function to end the created process. The function format is as follows:
TerminateProcess(handle, exitCode)
handle: the process handle to be operated
exitCode: the process exit code
Or use win32event.WaitForSingleObject to wait for the created thread to end. The function format is as follows :
WaitForSingleObject(handle, milisecond)
handle: The handle of the process to be operated
milisecond: The waiting time, if it is -1, keep waiting.
import win32process # 打开记事本,获得其句柄 handle = win32process.CreateProcess(r'C:\Windows\notepad.exe', '', None, None, 0, win32process.CREATE_NO_WINDOW, None, None, win32process.STARTUPINFO()) time.sleep(4) # 终止进程 win32process.TerminateProcess(handle[0], 0) import win32event #等待进程结束 print win32event.WaitForSingleObject(handle[0], -1)
4. Use ctypes to call functions in kernel32.dll
Using the ctypes module allows Python to call functions in the dynamic link library.
The ctypes module provides Python with the function of calling functions in dynamic link libraries. Using the ctypes module, you can easily call a dynamic link library written in C language and pass parameters to it.
The ctypes module defines the basic data types in C language and can implement structures and unions in C language. The ctypes module can work on multiple operating systems such as Windows, Linux, and Mac OS, and is basically cross-platform.
Example:
Call the MessageBoxA function in user32.dll under Windows.
from ctypes import * user32 = windll.LoadLibrary('user32.dll') a = user32.MessageBoxA(0, str.encode('Hello Ctypes!'), str.encode('Ctypes'), 0) print a
For more Python-related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to call other programs in python. For more information, please follow other related articles on the PHP Chinese website!