'If you os.open() and open() a file, you need to both close() and os.close() it?

I'm opening a file in Python using os.open(), and then I want to write to it using the file interface, ie. f.write().

So I'm doing the following

import os

fd = os.open('log', os.O_CREAT|os.O_RDWR, 0b111110110)
f  = open(fd,'w')

But when it's time to clean up, I don't know if I should do call both os.close(fd) and f.close(), or only one.

Calling os.close() xor f.close() is fine (ie. exactly one), but calling both throws a "bad file descriptor" error.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source