4

The following script:

import os

def call_close(fd):
    try:
        print fd
        os.close(fd)
    except Exception as e:
        print 'Exception:', e

for fd in range(10):
    call_close(fd)

prints

0
1

Nothing more. No exception. Any guess what is happening?

1

2 Answers 2

5

From the documentation (emphasis mine):

File descriptors are small integers corresponding to a file that has been opened by the current process. For example, standard input is usually file descriptor 0, standard output is 1, and standard error is 2.

The script isn't terminating; you're closing standard output, so anything printed after that line can't be displayed.

Sign up to request clarification or add additional context in comments.

Comments

4

os.close:

Close file descriptor fd.

Closing fd = 1 closes STDOUT so you will not see any more output.

1 Comment

Great. My real problem was that the logging stop working but the reason was the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.