I'd say mkstemp & os.close is clearer here too, though it's the same number of lines. I don't object to merging with this form if you prefer it, though.