python调用exe程序 传入参数_如何从Python调用windows.exe文件,输入参数并获得文件输出?...

这篇博客介绍了如何使用Python的subprocess模块来调用Windows下的exe程序,并向其传递参数。通过示例代码展示了如何处理stdin、stdout和stderr,确保正确通信并捕获返回码。
摘要由CSDN通过智能技术生成

通常命令行实用程序可以将其参数作为命令行参数接收,但是sir\util2通过C运行时函数^{}1从stdin读取用户输入。子进程便利函数call、check_call和{}不便于向stdin发送输入。直接使用Popen类,然后使用communicate。在

我手动跟踪源文件sir_util2.c中的fscanf调用,得出以下结果:import subprocess

sir_util2_path = 'sir_util2.exe'

def sir_to_geotiff(infname, outfname, smin, smax, show_nodata):

show_nodata = int(show_nodata)

opt = 6 # convert to image

fmt = 3 # GeoTIFF

param = [infname, opt, fmt, smin, smax, show_nodata, outfname]

cmd = [sir_util2_path]

p = subprocess.Popen(cmd,

stdin=subprocess.PIPE,

stdout=subprocess.PIPE,

stderr=subprocess.PIPE,

universal_newlines=True)

out, err = p.communicate('\n'.join(map(str, param)))

if p.returncode != 0:

raise subprocess.CalledProcessError(p.returncode,

cmd,

output=(out,err))

我使用了一个小的测试程序来代替编译原始源代码,所以

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值