How to get only the returned value from a function, in the below example, I want to get only foorbar and not this is a test in the bash shell.
$ cat foo.py
print "this is a test"
def bar():
return "foobar"
$ output=$(python -c 'import foo;print foo.bar()')
$ echo $output
this is a test foobar
-
.... suppress stdout, then re-enable it only for that function call maybe?juanpa.arrivillaga– juanpa.arrivillaga2018年02月09日 18:44:32 +00:00Commented Feb 9, 2018 at 18:44
3 Answers 3
I'd recommend if you require for whatever reason the print "this is a test" section, you check if the file is being run itself, such as by checking the __name__ property as follows:
foo.py:
def bar():
return "foobar"
if __name__ == "__main__":
print("this is a test")
This should mean that the only time "this is a test" is printed is when you call python foo.py, and not when foo is imported as a module.
Hope this helps!
1 Comment
If you want to keep the module as-is and get what you want, you need to suppress the module level string being printed by temporarily redirecting the STDOUT to someplace else (e.g. None, /dev/null or any other file descriptor as a dumping zone), and duplicate again to the original STDOUT once the import is done.
% cat spam.py
print("this is a test")
def bar():
return "foobar"
% python3 -c 'import sys;sys.stdout=None;import spam;sys.stdout=sys.__stdout__;print(spam.bar())'
foobar
% out=$(python3 -c 'import sys;sys.stdout=None;import spam;sys.stdout=sys.__stdout__;print(spam.bar())')
% echo "$out"
foobar
Expanded form:
import sys
sys.stdout = None
import spam
sys.stdout = sys.__stdout__
print(spam.bar())
4 Comments
None for sys.stdout without complaint from Python.write method that doesn't do anything: sys.stdout = type("DummyStdout", (object,), {"write": lambda self, s: None})()In your specific case, where the desired output is the final line of the python's stdout, you can use tail:
output=$(python -c 'import foo;print foo.bar()' | tail -1)