【问题标题】:Calling external *.exe with Python with arguments, no errors but getting no output使用带参数的 Python 调用外部 *.exe,没有错误但没有输出
【发布时间】:2019-07-19 03:29:27
【问题描述】:

我有 *.exe 程序,它需要简单的命令来创建用户(在 cmd 中): 我打开 cmd,导航到 my.exe 位置,然后像这样运行它:

my.exe cu 用户名密码邮箱

cu - 是 my.exe 用来创建用户的命令。

由于我有很多用户要创建,我想用 python 运行 my.exe。

我没有收到任何错误,但未创建用户。我已经在cmd中手动完成了,没有问题。由于我对python的这个模块不熟悉,所以我不太明白这是怎么回事。

我的脚本:

# import details for users from csv and write them to a list:
import csv
with open(r'C:\temp\test.csv', 'rb') as f:
    reader = csv.reader(f)
    users_list = list(reader) # list of lists

# run my.exe for each list entry, each is a list as well
import subprocess
for each in users_list:
    arguments = 'cu' +" "+ str(each[0])  +" "+ str(each[1]) +" "+ str(each[2])
    subprocess.call([r"C:\Software\ikfbatool\ikfbatool.exe", arguments])

【问题讨论】:

  • 您错误地使用了subprocess.call()。您必须将参数作为单独的列表项传递,而不是作为一个大字符串传递。即类似subprocess.call(['command.exe', 'arg1', 'arg2', 'arg3'])

标签: python python-2.7 subprocess


【解决方案1】:

您希望将参数作为长字符串传递,但这是不正确的。在您的情况下,您的列表将包含 2 个元素,只有 .exe 和一个包含参数的字符串。

命令的所有成员都应该在列表中分开。

你应该试试:

arguments = ['cu', str(each[0]), str(each[1]), str(each[2])]
subprocess.call([r"C:\Software\ikfbatool\ikfbatool.exe"].extend(arguments))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2011-05-11
    • 2012-08-23
    • 2013-02-02
    相关资源
    最近更新 更多