python运行其他程序有哪些方法?


本文摘自php中文网,作者零下一度,侵删。

python运行(调用)其他程序或脚本

在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码。为了更好地控制运行的进程,可以使用win32process模块中的函数。如果想进一步控制进程,则可以使用ctype模块,直接调用kernel32.dll中的函数。

1 使用os.system函数运行其他程序
2 使用ShellExecute函数运行其他程序
3 使用CreateProcess函数运行其他程序
4 使用ctypes调用kernel32.dll中的函数

python运行其他程序的实现方法

这里提供了两种实现方法,一.os.system()函数和 使用ShellExecute函数运行其他程序及实现代码,大家可以参考下,

一 使用os.system()函数运行其他程序

打开系统的记事本程序

1

2

3

4

5

>>>import os

>>> os.system('notepad')

0

>>> os.system('notepad python.txt')

0

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

1

2

3

4

5

6

7

8

9

10

11

12

13

>>>import win32api

>>> win32api.ShellExecute(0,'open','notepad.exe','','',0)

42

>>> win32api.ShellExecute(0,'open','notepad.exe','','',1)

42

>>> win32api.ShellExecute(0,'open','notepad.exe','python.txt','',1)

42

>>> win32api.ShellExecute(0,'open','http://www.python.org','python.txt','',1)

42

>>> win32api.ShellExecute(0,'open','E:\\python\\work\\Demo.mp3','','',1)

42

>>> win32api.ShellExecute(0,'open','E:\\python\\work\\MessageBox.py','','',1)

42

以上就是python运行其他程序有哪些方法?的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python怎么念

Python中的mkdir方法怎么用

Python中encoding是什么意思

Python常用类型转换实现

9种Python web程序的部署方式小结

Python中tornado防止跨站攻击的方法介绍

Python接口使用opencv的方法

Python中注释一般为什么颜色

Python适合游戏开发吗

Python的random怎么用

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...