欢迎光临
我们一直在努力

如何在python中调用外部程序

在python中调用外部程序的方法:1.使用os.system()函数调用;2.使用ShellExecute函数调用;3.使用ctypes模块调用;

具体方法如下:

1.使用os.system()函数调用

python中可以使用os.system()函数方便地运行其他程序或者脚本。

#打开记事本

os.system('notepad')

#用记事本打开aa.txt

os.system('notepad aa.txt')

#直接打开aa.txt

os.system('aa.txt')

#直接打开Excel文件

os.system('便宜香港vpsaa.xlsx')

#直接打开Word文件

os.system('bb.docx')

2.使用ShellExecute函数调用

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浏览器

3.使用ctypes模块调用

python中可以使用ctypes模块来调用位于动态链接库的函数。

//调用user32.dll中的MessageBoxA函数

from ctypes import *

user32 = windll.LoadLibrary('user32.dll')

a = user32.MessageBoxA(0, str.encode('Hello Ctypes!'), str.encode('Ctypes'), 0)

print a

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。