all Python 3osfunction
os.close
os — file descriptors
Close a file descriptor.
Syntax
os.close(fd)
Closing twice raises OSError, but the worse danger is that the number may already belong to another open file, and a stray close shuts that one instead. If the descriptor is wrapped in a file object via os.fdopen or open(fd), close it through the object — otherwise the object will later close a number that is no longer its own.
Examples
import os
r, w = os.pipe()
print(os.close(r) is None) # → True
os.close(w)
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy