Why do you think this is inefficient? Did you test it? By the way, it does not work at all because you are using the from ... import statement. Replacing sys.stdout is fine, but don't make a copy and don't use a temporary file. Open the null device instead:
import sys import os def foo(): print "abc" old_stdout = sys.stdout sys.stdout = open(os.devnull, "w") try: foo() finally: sys.stdout.close() sys.stdout = old_stdout