[英]how to understand sys.stdout and sys.stderr in python
I have the following simple python code. 我有以下简单的python代码。
stdout = sys.stdout
stderr = sys.stderr
try:
# omited
finally:
sys.stdout = stdout
After searching, I found that sys.stdin, sys.stdout, and sys.stderr are file objects corresponding to the interpreter's standard input, standard output and standard error streams. 搜索之后,我发现sys.stdin,sys.stdout和sys.stderr是与解释器的标准输入,标准输出和标准错误流相对应的文件对象。 So, my guess here is that we first assign the sys.stdout and sys.stderr to the variables stdout and stderr.
因此,我的猜测是,我们首先将sys.stdout和sys.stderr分配给变量stdout和stderr。 Then, even if there are exceptions in the try statement, we cal always get the sys.stdout back?
然后,即使try语句中有异常,我们也总是可以找回sys.stdout吗? Is this correct?
这个对吗? I felt that I was still confused.
我感到自己仍然很困惑。 Many thanks for your time and attention.
非常感谢您的时间和关注。
In normal C programs, there are three "files" open by default: stdin, stdout, and stderr. 在普通的C程序中,默认情况下打开三个“文件”:stdin,stdout和stderr。 When you do input and output in C, by default they come from stdin and stdout.
在C语言中进行输入和输出时,默认情况下它们来自stdin和stdout。 But, you can also use them where code expects files, or reassign them to be new files.
但是,您也可以在代码需要文件的地方使用它们,或将它们重新分配为新文件。
Python seeks to "mimic" this behavior of C. When you print()
in Python, your text is written to Python's sys.stdout
. Python试图“模仿” C的这种行为。在Python中使用
print()
时, sys.stdout
文本写入Python的sys.stdout
。 When you do input()
, it comes from sys.stdin
. 当您执行
input()
,它来自sys.stdin
。 Exceptions are written to sys.stderr
. 异常被写入
sys.stderr
。
You can reassign these variables in order to redirect the output of your code to a file other than stdout. 您可以重新分配这些变量,以将代码的输出重定向到stdout以外的文件。 This is very similar to shell redirection , if you're familiar with that.
如果您很熟悉,这与shell重定向非常相似。 The reason you might do something like this is to keep a log of your program's output or make code "shut up", ie not send output to stdout.
您可能这样做的原因是保留程序输出的日志或使代码“关闭”,即不将输出发送到stdout。 So, in your example example:
因此,在您的示例示例中:
stdout = sys.stdout
try:
sys.stdout = open('file.txt', 'w')
print('blah')
# etc
finally:
sys.stdout.close() # close file.txt
sys.stdout = stdout
sys.stderr = stderr
This code wouldn't print anything to the console, but it would write "blah" to a text file named file.txt
. 该代码不会在控制台上打印任何内容,但是会将“ blah”写入名为
file.txt
的文本文件。 To make this sort of thing less error-prone, Python provides sys.__stdin__
and sys.__stdout__
, which always hold the original values of sys.stdin
and sys.stdout
. 为了使这种事情更不易出错,Python提供了
sys.__stdin__
和sys.__stdout__
,它们始终保留sys.stdin
和sys.stdout
的原始值。 The above code could be made simpler using this: 使用以下代码可以使上面的代码更简单:
try:
sys.stdout = open('file.txt', 'w')
print('blah')
# etc
finally:
sys.stdout.close() # close file.txt
sys.stdout = sys.__stdout__
The reason Python has both stdout
and __stdout__
is mainly for convenience, so you don't have to make a variable to back up stdout
. Python同时具有
stdout
和__stdout__
的原因主要是为了方便起见,因此您不必创建变量来备份stdout
。
However, I have to recommend that you don't make a habit out of reassigning stdout
. 但是,我必须建议您不要因为重新分配
stdout
养成习惯。 It's pretty easy to mess things up that way! 这样弄乱是很容易的! If you want to save your code's output, you can use shell redirection.
如果要保存代码的输出,可以使用Shell重定向。 If you want to have the ability to keep a log of what your program is doing, see Python's logging module.
如果您想保留程序执行情况的日志 ,请参阅Python的日志记录模块。 If you want your code to be quieter, give your functions a
quiet=False
parameter so you can shut them up when you want! 如果您希望代码更安静,请为您的函数提供一个
quiet=False
参数,以便您可以在需要时将其关闭! There is rarely a "genuine" need to do reassign stdout
, etc. Python allows you to do it, because Python gives programmers a lot of control, and expects them to be responsible. 重新分配
stdout
等几乎没有“真正的”需要。Python允许您这样做,因为Python为程序员提供了很多控制权,并希望他们负责。 You could do something like this, if you really wanted to: 如果您确实想执行以下操作,则可以执行以下操作:
>>> import random
>>> random.random = lambda: 1
>>> random.random()
1
>>> random.random()
1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.