Created
November 10, 2013 08:34
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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