Simple. *call* function in python. How to fix the return?

爱⌒轻易说出口 提交于 2019-12-23 01:42:30

问题


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?


回答1:


You want to use Popen with communicate, as described in the docs:

>>> from subprocess import *
>>> Popen(['echo', 'Hi'], stdout=PIPE).communicate()[0]
'Hi\n'



回答2:


The value you are seeing is the return value of the echo process. Because you are in a active interpreter this return value gets printed to the terminal. You can ignore it by assigning it to a dummy variable:

_ = call(['echo','Hi'])

Just making sure, you do know you can just write print("Hi"), right?



来源:https://stackoverflow.com/questions/10290157/simple-call-function-in-python-how-to-fix-the-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!