Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(13321)

Side by Side Diff: Doc/library/tempfile.rst

Issue 5178: Add context manager for temporary directory
Patch Set: Created 8 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/tempfile.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 :mod:`tempfile` --- Generate temporary files and directories 2 :mod:`tempfile` --- Generate temporary files and directories
3 ============================================================ 3 ============================================================
4 4
5 .. sectionauthor:: Zack Weinberg <zack@codesourcery.com> 5 .. sectionauthor:: Zack Weinberg <zack@codesourcery.com>
6 6
7 7
8 .. module:: tempfile 8 .. module:: tempfile
9 :synopsis: Generate temporary files and directories. 9 :synopsis: Generate temporary files and directories.
10 10
(...skipping 11 matching lines...) Expand all
22 insecure :func:`mktemp` function. Temporary file names created by this module 22 insecure :func:`mktemp` function. Temporary file names created by this module
23 no longer contain the process ID; instead a string of six random characters is 23 no longer contain the process ID; instead a string of six random characters is
24 used. 24 used.
25 25
26 Also, all the user-callable functions now take additional arguments which 26 Also, all the user-callable functions now take additional arguments which
27 allow direct control over the location and name of temporary files. It is 27 allow direct control over the location and name of temporary files. It is
28 no longer necessary to use the global *tempdir* and *template* variables. 28 no longer necessary to use the global *tempdir* and *template* variables.
29 To maintain backward compatibility, the argument order is somewhat odd; it 29 To maintain backward compatibility, the argument order is somewhat odd; it
30 is recommended to use keyword arguments for clarity. 30 is recommended to use keyword arguments for clarity.
31 31
32 The module defines the following user-callable functions: 32 The module defines the following user-callable items:
33 33
34 34
35 .. function:: TemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[ , dir=None]]]]]) 35 .. function:: TemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[ , dir=None]]]]])
36 36
37 Return a file-like object that can be used as a temporary storage area. 37 Return a file-like object that can be used as a temporary storage area.
38 The file is created using :func:`mkstemp`. It will be destroyed as soon 38 The file is created using :func:`mkstemp`. It will be destroyed as soon
39 as it is closed (including an implicit close when the object is garbage 39 as it is closed (including an implicit close when the object is garbage
40 collected). Under Unix, the directory entry for the file is removed 40 collected). Under Unix, the directory entry for the file is removed
41 immediately after the file is created. Other platforms do not support 41 immediately after the file is created. Other platforms do not support
42 this; your code should not rely on a temporary file created using this 42 this; your code should not rely on a temporary file created using this
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 144
145 The user of :func:`mkdtemp` is responsible for deleting the temporary 145 The user of :func:`mkdtemp` is responsible for deleting the temporary
146 directory and its contents when done with it. 146 directory and its contents when done with it.
147 147
148 The *prefix*, *suffix*, and *dir* arguments are the same as for 148 The *prefix*, *suffix*, and *dir* arguments are the same as for
149 :func:`mkstemp`. 149 :func:`mkstemp`.
150 150
151 :func:`mkdtemp` returns the absolute pathname of the new directory. 151 :func:`mkdtemp` returns the absolute pathname of the new directory.
152 152
153 .. versionadded:: 2.3 153 .. versionadded:: 2.3
154
155
156 .. class:: TemporaryDirectory([suffix=''[, prefix='tmp'[, dir=None]]])
157
158 Creates a temporary directory using :func:`mkdtemp`. The class can
159 used as a context manager (see :ref:`context-managers`). On exiting
160 of the context, the temporary directory and all its contents are
161 removed. The directory can be manually be cleaned up by calling the
162 :func:`cleanup` method. The directory name can be retrieved from the
163 :attr:`name` member of the object.
164
165 .. versionadded:: 2.7
154 166
155 167
156 .. function:: mktemp([suffix=''[, prefix='tmp'[, dir=None]]]) 168 .. function:: mktemp([suffix=''[, prefix='tmp'[, dir=None]]])
157 169
158 .. deprecated:: 2.3 170 .. deprecated:: 2.3
159 Use :func:`mkstemp` instead. 171 Use :func:`mkstemp` instead.
160 172
161 Return an absolute pathname of a file that did not exist at the time the 173 Return an absolute pathname of a file that did not exist at the time the
162 call is made. The *prefix*, *suffix*, and *dir* arguments are the same 174 call is made. The *prefix*, *suffix*, and *dir* arguments are the same
163 as for :func:`mkstemp`. 175 as for :func:`mkstemp`.
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 255
244 256
245 .. function:: gettempprefix() 257 .. function:: gettempprefix()
246 258
247 Return the filename prefix used to create temporary files. This does not 259 Return the filename prefix used to create temporary files. This does not
248 contain the directory component. Using this function is preferred over readi ng 260 contain the directory component. Using this function is preferred over readi ng
249 the *template* variable directly. 261 the *template* variable directly.
250 262
251 .. versionadded:: 1.5.2 263 .. versionadded:: 1.5.2
252 264
265
266 Examples
267 --------
268
269 Here are some examples of typical usage of the :mod:`tempfile` module::
270
271 >>> import tempfile
272
273 # create a temporary file and write some data to it
274 >>> fp = tempfile.TemporaryFile()
275 >>> fp.write('Hello world!')
276 # read data from file
277 >>> fp.seek(0)
278 >>> fp.read()
279 'Hello world!'
280 # close the file, it will be removed
281 >>> fp.close()
282
283 # create a temporary file using a context manager
284 >>> with tempfile.TemporaryFile() as fp:
285 ... fp.write('Hello world!')
286 ... fp.seek(0)
287 ... fp.read()
288 'Hello world!'
289 >>>
290 # file is now closed and removed
291
292 # create a temporary directory using the context manager
293 >>> with tempfile.TemporaryDirectory() as tmpdirname:
294 ... print 'created temporary directory', tmpdirname
295 >>>
296 # directory and contents have been removed
297
OLDNEW
« no previous file with comments | « no previous file | Lib/tempfile.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+