问题
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