python主程序循环调用子程序_如何用python调用子程序

本文详细介绍了在Python中如何调用子程序,包括使用os.system()、shellexecute、createprocess以及ctypes调用kernel32.dll函数的四种方法。通过这些方法,开发者可以灵活地运行其他脚本、程序或控制进程。

2018-01-10 回答

在python中,可以方便地使用os模块来运行其他脚本或者程序,这样就可以在脚本中直接使用其他脚本或程序提供的功能,而不必再次编写实现该功能的代码。为了更好地控制运行的进程,

可以使用win32process模块中的函数,如果想进一步控制进程,则可以使用ctype模块,直接调用kernel32.dll中的函数。下面介绍4种方式:

1、os.system()函数

os模块中的system()函数可以方便地运行其他程序或者脚本,模式如下:

os.system(command):command: 要执行的命令,如果要向脚本传递参数,可以使用空格分割程序及多个参数

实例:

#打开记事本os.system('notepad')#用记事本打开aa.txtos.system('notepad aa.txt') #aa.txt文件必须在当前程序目录#直接打开aa.txtos.system('aa.txt')#直接打开excel文件os.system('aa.xlsx')#直接打开word文件os.system('bb.docx')

filepath='测试.xlsx'#打开包含中文的文件os.system(filepath.decode('utf8').encode('gbk'))

2、shellexecute函数

使用win32api模块中的shellexecute()函数来运行其他程序,格式如下

shellexecute(hwnd, op, file, args, dir, show)

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

op  :          要运行的操作,为open,print或者为空

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

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

dir :          程序初始化的目录

show:          是否显示窗口

使用shellexecute函数,就相当于在资源管理器中双击文件图标,系统会打开相应程序运行。

引用win32api,需要安装 pywin32, https://sourceforge.net/projects/pywin32/files/pywin32/

实例:

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

参考实例:https://www.programcreek.com/python/example/8489/win32process.createprocess

3.1、创建进程

为了便于控制通过脚本运行的程序,可以使用win32process模块中的createprocess()函数创建一个运行相应程序的进程。其函数格式为:

createprocess(appname, cmdline, proattr, threadattr, inherithandle, creationflags, newenv, currentdir, attr)

appname         可执行文件名

cmdline         命令行参数

procattr        进程安全属性

threadattr      线程安全属性

inherithandle  继承标志

creationflags  创建标志

currentdir      进程的当前目录

attr             创建程序的属性

3.2、结束进程

可以使用win32process.terminateprocess函数来结束已创建的进程, 函数格式如下:

terminateprocess(handle, exitcode)

handle     要操作的进程句柄

exitcode   进程退出代码

或者使用win32event.waitforsingleobject等待创建的线程结束,函数格式如下:

waitforsingleobject(handle, milisecond)

handle     : 要操作的进程句柄

milisecond: 等待的时间,如果为-1,则一直等待.

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、使用ctypes调用kernel32.dll中的函数

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

ctypes模块为python提供了调用动态链接库中函数的功能。使用ctypes模块可以方便地调用由c语言编写的动态链接库,并向其传递参数。

ctypes模块定义了c语言中的基本数据类型,并且可以实现c语言中的结构体和联合体。ctypes模块可以工作在windows,linux,mac os等多种操作系统,基本上实现了跨平台。

实例:

windows下调用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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值