What is an easy way to implement fprintf in python?

In python, the following will let us write to stdout:

import sys
sys.stdout.write("blah %d" % 5)

However, I want to be flexible about which stream we print to. I want to pass the stream as an argument into a function, as you would when calling fprintf in any of C derived languages. In the snippet of source code above, stdout is hard-coded into the write-statement. However, we want to be flexible about which stream we write to. we might want stderr, or some other stream instead of stdout.

Leave a Comment