Reputation: 117
I am new to Python and having some trouble with the stderr.write
function. I will try to illustrate it with code. Before I was doing this:
print "Unexpected error! File {0} could not be converted." .format(src)
But then I wanted to separate the error messages from other status messages so I tried doing this:
sys.stderr.write "Unexpected error! File %s could not be converted." src
But this results in error. I googled it as well but i couldn't find anything. Could anyone please help me out here. How can I print the string src
using stderr.write
?
Upvotes: 3
Views: 9255
Reputation: 798436
Functions in Python need to be followed by parens ((...)
), optionally containing arguments, in order to be invoked.
sys.stderr.write("Unexpected error! File %s could not be converted.\n" % (src,))
Upvotes: 1
Reputation: 29093
You have missed (,) and %:
import sys
sys.stderr.write("Unexpected error! File %s could not be converted." % src)
Upvotes: 0
Reputation: 879073
sys.stderr.write
is a function, so to call the function, you need to use parentheses around the argument:
In [1]: src='foo'
In [2]: sys.stderr.write("Unexpected error! File %s could not be converted."%src)
Unexpected error! File foo could not be converted.
Note that as of Python3, print
is also a function and will also require parentheses.
Upvotes: 0
Reputation: 824
In Python 2.x:
sys.stderr.write("Unexpected error! File %s could not be converted." % src)
Or, in Python 2.x and 3.x:
sys.stderr.write("Unexpected error! File {0} could not be converted.".format(src))
Upvotes: 8