标准错误.write;打印字符串
我是 Python 新手,在使用 stderr.write
函数时遇到一些问题。我将尝试用代码来说明它。在我这样做之前:
print "Unexpected error! File {0} could not be converted." .format(src)
但是后来我想将错误消息与其他状态消息分开,所以我尝试这样做:
sys.stderr.write "Unexpected error! File %s could not be converted." src
但这会导致错误。我也用谷歌搜索了它,但我找不到任何东西。有人可以帮我吗?如何使用 stderr.write
打印字符串 src
?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Python 2.x 中:
或者,在 Python 2.x 和 3.x 中:
In Python 2.x:
Or, in Python 2.x and 3.x:
Python 中的函数后面需要跟括号 (
(...)
),可选地包含参数,以便被调用。Functions in Python need to be followed by parens (
(...)
), optionally containing arguments, in order to be invoked.sys.stderr.write
是一个函数,因此要调用该函数,需要在参数两边使用括号:请注意,从 Python3 开始,
print
也是一个函数,并且还需要括号。sys.stderr.write
is a function, so to call the function, you need to use parentheses around the argument:Note that as of Python3,
print
is also a function and will also require parentheses.您错过了 (,) 和 %:
You have missed (,) and %: