Python调用(运行)外部程序参数问题
来源:互联网 发布:天猫和淘宝关系 编辑:程序博客网 时间:2024/05/16 18:50
ShellExecute 与 CreateProcess
ShellExecute
ShellExecute(hwnd, op , file , params , dir , bShow )
其参数含义如下所示。
hwnd:父窗口的句柄,如果没有父窗口,则为0。
op:要进行的操作,为“open”、“print”或者为空。
file:要运行的程序,或者打开的脚本。
params:要向程序传递的参数,如果打开的为文件,则为空。
dir:程序初始化的目录。
bShow:是否显示窗口
可以这么理解,使用ShellExecute就像在windows下的cmd中调用某个exe文件
CreateProcess
为了便于控制通过脚本运行的程序,可以使用win32process模块中的CreateProcess()函数。其函数原型如下所示。
CreateProcess(appName, commandLine , processAttributes , threadAttributes , bInheritHandles ,dwCreationFlags , newEnvironment , currentDirectory , startupinfo )
其参数含义如下。
appName:可执行的文件名。
commandLine:命令行参数。
processAttributes:进程安全属性,如果为None,则为默认的安全属性。
threadAttributes:线程安全属性,如果为None,则为默认的安全属性。
bInheritHandles:继承标志。
dwCreationFlags:创建标志。
newEnvironment:创建进程的环境变量。
currentDirectory:进程的当前目录。
startupinfo :创建进程的属性。
CreateProcess调用exe与ShellExecute调用略有不同,主要是参数的传递不同。接下来以例子说明
Example
编写一段简单的C++代码,并生成exe文件,代码如下
#include <iostream>#include <stdio.h>using namespace std;int main(int argc, char* argv[]){ if (argc < 2) { cerr << "hello nothing" << endl; cin.get(); return -1; } for (int i = 1; i < argc; ++i) { cout << argv[i] << endl; } cin.get(); return 0;}
使用ShellExecute调用
#usr/bin/env python#-*- coding: utf-8 -*-import win32apiimport win32eventimport win32processdef main(): exePath = "E:\python_wk\callExeOnWin\helloSomething.exe" param = "hello world abc 123" win32api.ShellExecute(0, "open", exePath, param, '', 1)if '__main__' == __name__: main()
可以看到参数用空格隔开即可
使用CreateProcess调用
#usr/bin/env python#-*- coding: utf-8 -*-import win32apiimport win32eventimport win32processdef main(): exePath = "E:\python_wk\callExeOnWin\helloSomething.exe" param = "hello world abc 123" param = exePath + " " + param handle = win32process.CreateProcess(exePath, param, None, None, 0, win32process.CREATE_NEW_CONSOLE, None, None, win32process.STARTUPINFO())if '__main__' == __name__: main()
因为由C++编译的exe文件的第一个参数是自己本身,也就是在C++程序中 argv[0] 的值是exe文件本身的路径,可以看到,在使用CreateProcess调用exe时,我们需要将exe路径加入到参数中,而在ShellExecute中是不需要的。
- Python调用(运行)外部程序参数问题
- Python调用(运行)外部程序
- Python调用(运行)外部程序
- Python调用(运行)外部程序
- Python调用(运行)外部程序
- Python调用(运行)外部程序
- Python调用(运行)外部程序
- Python调用(运行)外部程序
- Python调用(运行)外部程序
- Python调用(运行)外部程序
- (转)Python调用(运行)外部程序
- Python调用外部程序
- python调用外部程序
- Python调用外部程序
- python从文件外部调用参数
- Python执行带参数的外部程序
- python中使用subprocess调用外部程序
- python中使用subprocess调用外部程序
- HDOJ 5591 ZYB's Game
- 12月新的一月~关于学习CRON指令相关的东西
- 小知识4 在根视图上设置回收键盘响应,点击空白处回收键盘
- nginx源码解读之一--开局
- 【springmvc+mybatis项目实战】杰信商贸-18.附件分类基础表+数据字典
- Python调用(运行)外部程序参数问题
- 【springmvc+mybatis项目实战】杰信商贸-19.级联删除
- xxx is not in the sudoers file.This incident will be reported.的解决方法
- 【springmvc+mybatis项目实战】杰信商贸-20.合同货物数和附件数
- Hadoop 2.6.0+Hbase1.12+mahout0.9 集群搭建
- 递归-旅行员售货
- 【springmvc+mybatis项目实战】杰信商贸-21.合同总金额SQL
- 【springmvc+mybatis项目实战】杰信商贸-22.合同货物附件生产厂家mapper关联
- Android 关联suppor-V4包源码