简单。 *在Python中调用*函数。如何解决退货问题? [英] Simple. *call* function in python. How to fix the return?
本文介绍了简单。 *在Python中调用*函数。如何解决退货问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ python
>>> from subprocess import *
>>> call(['echo','Hi'])
Hi
0
我的问题看起来很简单。在通话结束时我不想要这个 0 。
由 call 调用的每个函数都与此一起出现,这会混淆有条件的测试。
类似于:
$ p $
if int(call(['function','parameter']))> 10:
print'yes'
所以我怎样才能收到
在这个例子中,您好
解决方案
您希望使用Popen与沟通
,如文档中所述:
>>> from subprocess import *
>>> Popen(['echo','Hi'],stdout = PIPE).communicate()[0]
'Hi'\
'
See this simple code:
$ python
>>> from subprocess import *
>>> call(['echo','Hi'])
Hi
0
My problem looks simple. I don't want this 0 at the end of the call. Every function called by call appears with this and this messes up things for conditional tests.
Something like:
if int(call(['function', 'parameter']))>10:
print 'yes'
So how can I receive only
Hi
in this example?
解决方案
You want to use Popen with communicate
, as described in the docs:
>>> from subprocess import *
>>> Popen(['echo', 'Hi'], stdout=PIPE).communicate()[0]
'Hi\n'
这篇关于简单。 *在Python中调用*函数。如何解决退货问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文