In Python 3, it is possible to open a file object using an "integer file descriptor" with the format:
stdout = open(1, "w")
stdout.write("Hello World") # Prints Hello World
stdout.close()
Though, interestingly, I found that 0
is also a valid stream.
If I put this in the file testio.py
:
stdout = open(0, "w")
stdout.write("Foo Bar
")
stdout.close()
And then run that code the output is:
bash-3.2$ python3 testio.py
Foo Bar
Which seems just like stdout
. However...
bash-3.2$ python3 testio.py > testio.txt
Foo Bar
bash-3.2$ cat testio.txt
So it seems that this is actually not stdout
, but something else.
And it does not appear to be stderr
either:
bash-3.2$ python3 testio.py 2> testio.txt
Foo Bar
bash-3.2$ cat testio.txt
However, I did find that the output can be redirected using 0>
:
bash-3.2$ python3 testio.py 0> testio.txt
bash-3.2$ cat testio.txt
Foo Bar
So my question is, what exactly does open(0, "w")
due? And what is this "0>" stream that is being redirected?
Python 3.6.5
Bash 3.2
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…