Integer File Descriptor “0” in open()

前端 未结 3 596
抹茶落季
抹茶落季 2020-12-03 14:52

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\")         


        
3条回答
  •  不思量自难忘°
    2020-12-03 15:41

    File handle 0 is stdin. Without redirection stdout, stderr, and stdin are all pointing to the terminal (so will all act the same). However when redirection is used, they will behave differently, because they will no longer be the same.

    I.E. If you do python3 testio.py 2> testio.txt, then stdout goes to the file, but stdin is still the terminal.

    This is just a bi-product of there being no checking to see that you only read stdin, and only write stdout, and stderr.

提交回复
热议问题