Skip to content

Instantly share code, notes, and snippets.

@abn
Created November 10, 2013 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abn/7395476 to your computer and use it in GitHub Desktop.
Save abn/7395476 to your computer and use it in GitHub Desktop.
[python] secure tempfile creation/use usual examples out there seems to ignore the file descriptor returned.
from os import fdopen, remove
from tempfile import mkstemp
# http://docs.python.org/2/library/tempfile.html#tempfile.mkstemp
fd, filename = mkstemp(prefix='example')
# note the usage of fdopen/fd instead of open/filename
with fdopen(fd, 'w') as f:
# do something with the file
pass
# clean up after yourself, mkstemp does not delete the created file
remove(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment