在Python中使用subprocess.check_output时出错

时间:2013-11-24 17:38:07

标签: python subprocess

我在Python中处于非常基础的水平,我正在尝试学习如何使用子进程模块。我有一个名为x.py的简单计算器程序,它接受一个数字,乘以2,然后返回结果。我试图用以下两行代码从IDLE执行那个简单的程序,但是我得到了错误。数字5是我试图输入x.py以获得结果的数字。有人会介意帮助我理解我做错了什么并帮助我做对吗?谢谢!

import subprocess

result = subprocess.check_output(["C:\\Users\\Kyle\\Desktop\\x.py",5])

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
result = subprocess.check_output(["C:\\Users\\Kyle\\Desktop\\x.py",5])
File "C:\Python27\lib\subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 855, in _execute_child
args = list2cmdline(args)
File "C:\Python27\lib\subprocess.py", line 587, in list2cmdline
needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'int' is not iterable

1 个答案:

答案 0 :(得分:0)

5作为字符串传递:

import sys
result = subprocess.check_output([sys.executable, "C:\\Users\\Kyle\\Desktop\\x.py", '5'])