简体   繁体   English

在Python中使用subprocess.check_output时出错

[英]Error using subprocess.check_output in Python

I am at a very basic level in Python, and I'm trying to learn how to use the subprocess module. 我在Python方面处于非常基础的水平,我正在尝试学习如何使用子流程模块。 I have a simple calculator program called x.py that takes in a number, multiplies it by 2, and returns the result. 我有一个名为x.py的简单计算器程序,该程序接受一个数字,将其乘以2,然后返回结果。 I am trying to execute that simple program from IDLE with the following two lines of code, but I get errors. 我试图使用以下两行代码从IDLE执行该简单程序,但出现错误。 The number 5 is the number I'm trying to feed into x.py to get a result. 数字5是我试图输入x.py以获取结果的数字。 Would someone mind helping me understand what I'm doing wrong and help me get it right? 有人会介意帮助我了解我做错了什么并帮助我弄对了吗? Thanks! 谢谢!

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

Pass 5 as a string: 5作为字符串传递:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM