使用ShellExecute函数运行其他程序 ——转载

10.4.2  使用ShellExecute函数运行其他程序

除了使用os模块中的os.system()函数以外,还可以使用win32api模块中的ShellExecute()函数。其函数如下所示。

ShellExecute(hwnd, op , file , params , dir , bShow )

其参数含义如下所示。

hwnd:父窗口的句柄,如果没有父窗口,则为0。

op:要进行的操作,为“open”、“print”或者为空。

file:要运行的程序,或者打开的脚本。

params:要向程序传递的参数,如果打开的为文件,则为空。

dir:程序初始化的目录。

bShow:是否显示窗口。

以下实例使用ShellExecute函数运行其他程序。

扫描二维码关注公众号,回复: 4408461 查看本文章

>>> import win32api
# 打开记事本程序,在后台运行,即显示记事本程序的窗口
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', '','',0)
 

# 打开记事本程序,在前台运行
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', '','',1)
 

# 向记事本传递参数,打开python.txt
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', 'python.txt','',1)
 

# 在默认浏览器中打开http://www.python.org网站
>>> win32api.ShellExecute(0, 'open', 'http://www.python.org', '','',1)
 

# 在默认的媒体播放器中播放E:\song.wma
>>> win32api.ShellExecute(0, 'open', 'E:\\song.wma', '','',1)
 

# 运行位于E:\book\code目录中的MessageBox.py脚本
>>> win32api.ShellExecute(0, 'open', 'E:\\book\\code\\MessageBox.py', '','',1)
 

可以看出,使用ShellExecute函数,就相当于在资源管理器中双击文件图标一样,系统会打开相应的应用程序执行操作。

猜你喜欢

转载自blog.csdn.net/lshdp/article/details/83788812
今日推荐