diff options
author | Skip Montanaro <skip@pobox.com> | 2008-05-09 00:45:00 (GMT) |
---|---|---|
committer | Skip Montanaro <skip@pobox.com> | 2008-05-09 00:45:00 (GMT) |
commit | e404a12a46485641ec69db0cf3bd507e210b31b4 (patch) | |
tree | ffd1c188f15f66e2e4bf65ab06baa28d84593991 /Doc/library/tempfile.rst | |
parent | 0893a0a961c3393aea052b268b630a7e522ba3a9 (diff) | |
download | cpython-e404a12a46485641ec69db0cf3bd507e210b31b4.zip cpython-e404a12a46485641ec69db0cf3bd507e210b31b4.tar.gz cpython-e404a12a46485641ec69db0cf3bd507e210b31b4.tar.bz2 |
Add an example about using NamedTemporaryFile() to replace mktemp(). I'm
unclear whether the verbatim text should have been indented or by how much.
Diffstat (limited to 'Doc/library/tempfile.rst')
-rw-r--r-- | Doc/library/tempfile.rst | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index 59cca42..79fa391 100644 --- a/Doc/library/tempfile.rst +++ b/Doc/library/tempfile.rst @@ -167,6 +167,24 @@ The module defines the following user-callable functions: Use of this function may introduce a security hole in your program. By the time you get around to doing anything with the file name it returns, someone else may have beaten you to the punch. + :func:`mktemp` usage can be replaced easily with + :func:`NamedTemporaryFile`, passing it the `delete=False` parameter :: + + >>> f = NamedTemporaryFile(delete=False) + >>> print f.name + >>> f.write("Hello World!\n") + >>> f.close() + >>> os.unlink(f.name) + >>> f = NamedTemporaryFile(delete=False) + >>> f + <open file '<fdopen>', mode 'w+b' at 0x384698> + >>> f.name + '/var/folders/5q/5qTPn6xq2RaWqk+1Ytw3-U+++TI/-Tmp-/tmpG7V1Y0' + >>> f.write("Hello World!\n") + >>> f.close() + >>> os.unlink(f.name) + >>> os.path.exists(f.name) + False The module uses two global variables that tell it how to construct a temporary name. They are initialized at the first call to any of the |