Python运行外部executab

2024-04-20 16:33:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试运行一个外部可执行文件,它需要复杂的参数并将输出捕获到一个变量中。我该怎么做

import os
import subprocess
subprocess.call('C:\\bin\\test.exe', ' -b10.10.2000',' -house50.20E,10.40N',' -hsyE',' -utc00.18',' -eswe',' -sid27',' -fPls',' -head',' -g')


>>> subprocess.call('C:\\bin\\test.exe', ' -b10.10.2000',' -house50.20E,10.40N',' -hsyE',' -utc00.18',' -eswe',' -sid27',' -fPls',' -head',' -g')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 3
25, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 7
29, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
>>>

谢谢


Tags: intestimportbinlinecallexeusers
1条回答
网友
1楼 · 发布于 2024-04-20 16:33:25

输出是另一个进程的标准输出

可以使用subprocess.Popen和communicate(),然后:

 proc = subprocess.Popen( [ 'C:\\bin\\test.exe', '-b10.10.2000','-house50.20E,10.40N','-hsyE','-utc00.18','-eswe','-sid27','-fPls','-head','-g' ], stdout = subprocess.PIPE, stderr = subprocess.PIPE )
 out, err = proc.communicate() #out -> stdout, err -> stderr

相关问题 更多 >