I was thinking it would be better to use the function mkstemp from tempfile. See https://docs.python.org/2/library/tempfile.html#tempfile.mkstemp

See example of use:
https://github.com/inspirehep/inspire/blob/346e34074b42ee32341867f8b76f858a0de3852a/apsharvest/apsharvest_utils.py#L143