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

Delta Between Two Patch Sets: Doc/library/tempfile.rst

Issue 5178: Add context manager for temporary directory
Left Patch Set: Created 8 years, 11 months ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Lib/tempfile.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 # create a temporary file and write some data to it 273 # create a temporary file and write some data to it
274 >>> fp = tempfile.TemporaryFile() 274 >>> fp = tempfile.TemporaryFile()
275 >>> fp.write('Hello world!') 275 >>> fp.write('Hello world!')
276 # read data from file 276 # read data from file
277 >>> fp.seek(0) 277 >>> fp.seek(0)
278 >>> fp.read() 278 >>> fp.read()
279 'Hello world!' 279 'Hello world!'
280 # close the file, it will be removed 280 # close the file, it will be removed
281 >>> fp.close() 281 >>> fp.close()
282 282
283 # create a temporary directory using the context manager, it will be 283 # create a temporary file using a context manager
284 # removed when the context is exited 284 >>> with tempfile.TemporaryFile() as fp:
285 >>> with tempfile.TemporaryDirectory() as tmpdir: 285 ... fp.write('Hello world!')
286 ... print 'created temporary directory', tmpdir 286 ... fp.seek(0)
287 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
LEFTRIGHT
« no previous file | Lib/tempfile.py » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

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