Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Why doesn closing sys.stdout (stdin, stderr) really close it?

closing stderr stdin
0
10 Posted

Why doesn closing sys.stdout (stdin, stderr) really close it?

0

Python file objects are a high-level layer of abstraction on top of C streams, which in turn are a medium-level layer of abstraction on top of (among other things) low-level C file descriptors. For most file objects you create in Python via the builtin file constructor, f.close() marks the Python file object as being closed from Python’s point of view, and also arranges to close the underlying C stream. This also happens automatically in f’s destructor, when f becomes garbage. But stdin, stdout and stderr are treated specially by Python, because of the special status also given to them by C. Running sys.stdout.close() marks the Python-level file object as being closed, but does not close the associated C stream. To close the underlying C stream for one of these three, you should first be sure that’s what you really want to do (e.g., you may confuse extension modules trying to do I/O). If it is, use os.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.