Skip to main content
 首页 » 编程设计

python之使用 Python 运行带参数的可执行文件

2025年05月04日14yyy_WW

我正在编写一个小的 python 脚本,我在其中打开一个现有的可执行文件 (.exe) 并发送一个字符串作为参数。

我正在使用 subprocess.call 方法,但出现以下错误:

File "C:\Python34\lib\subprocess.py", line 537, in call 
   with Popen(*popenargs, **kwargs) as p: 
File "C:\Python34\lib\subprocess.py", line 767, in __init__ 
   raise TypeError("bufsize must be an integer") 
TypeError: bufsize must be an integer 

我的代码

import os 
import subprocess 
 
x = subprocess.call("C:\\Users\\Desktop\\Program\\Program.exe", y) 

其中 y 是我传递的字符串。

我正在尝试升级旧的 VB 代码。原始代码调用可执行文件并传递参数,如下所示。我正在尝试在 Python 中复制它。

Private comm As ExecCmd 
Dim cmd As String 
Dim app As String 
Dim e As New ExecCmd 
 
exec_1= "...\Desktop\Program.exe" 
x = "Text" & Variable & " Hello" & Variable2 
 
comm.StartApp exec_1, x   'starts the .exe file with an argument 

请您参考如下方法:

首先将程序和您想要的任何参数放入数组中。

import os 
import subprocess 
 
x = subprocess.call(["C:\\Users\\Desktop\\Program\\Program.exe", y])