好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python运行其他程序的方法实例详解

这篇文章主要介绍了Python实现运行其他程序的四种方式,结合实例形式分析了Python执行其他程序相关模块与函数使用技巧,需要的朋友可以参考下

>>> import os
>>> os.system('notepad')    # 打开记事本程序.
0
>>> os.system('notepad 1.txt') # 打开1.txt文件,如果不存在,则创建.
0 
>>> import win32api
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', '', '', 0)      # 后台执行
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', '', '', 1)      # 前台打开
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', '1.txt', '', 1)   # 打开文件
>>> win32api.ShellExecute(0, 'open', 'http://HdhCmsTestsohu测试数据', '', '', 1)  # 打开网页
>>> win32api.ShellExecute(0, 'open', 'D:\\Opera.mp3', '', '', 1)     # 播放视频
>>> win32api.ShellExecute(0, 'open', 'D:\\hello.py', '', '', 1)     # 运行程序 
>>> win32process.CreateProcess('C:\\Windows\\notepad.exe', '', None, None, 0, win32process.CREATE_NO_WINDOW,
None, None, win32process.STARTUPINFO())
(<PyHANDLE:892>, <PyHANDLE:644>, 21592, 18780) # 函数返回进程句柄、线程句柄、进程ID以及线程ID 
>>> import win32process
>>> handle = win32process.CreateProcess('C:\\Windows\\notepad.exe', '', None, None, 0, win32process
.CREATE_NO_WINDOW, None, None, win32process.STARTUPINFO())      # 打开记事本,获得其句柄
>>> win32process.TerminateProcess(handle[0], 0)            # 终止进程 
>>> import win32event
>>> handle = win32process.CreateProcess('C:\\Windows\\notepad.exe', '', None, None, 0,
win32process.CREATE_NO_WINDOW, None, None, win32process.STARTUPINFO()) # 创建进程获得句柄
>>> win32event.WaitForSingleObject(handle[0], -1)           # 等待进程结束
0                                   # 进程结束返回值 
>>> from ctypes import *
>>> user32 = windll.LoadLibrary('user32.dll')
>>> user32.MessageBoxA(0, str.encode('Ctypes is so smart!'), str.encode('Ctypes'), 0)
1 

ctype模块中含有的基本类型与C语言类似,下面是几个基本的数据类型的对照:

---------------------------------------
Ctypes数据类型 C数据类型
---------------------------------------
c_char char
c_short short
c_int int
c_long long
c_float float
c_doule double
c_void_p void *
---------------------------------------

以上就是Python运行其他程序的方法实例详解的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于Python运行其他程序的方法实例详解的详细内容...

  阅读:60次