The best way I know is to use tempfile.NamedTemporaryFile , as you can see in @tordek's answer.
But if you have the original FD from the base OS, you can do this to turn it into a file object:
f = open(fd, closefd=True)
This works in Python 3
l04m33
source share