Cross platform temporary file creation. This is an alternative to ``tempfile.NamedTemporaryFile`` that also works on windows and avoids the "file being used by another process" error.
(mode)
| 29 | |
| 30 | @contextlib.contextmanager |
| 31 | def temporary_file(mode): |
| 32 | """Cross platform temporary file creation. |
| 33 | |
| 34 | This is an alternative to ``tempfile.NamedTemporaryFile`` that |
| 35 | also works on windows and avoids the "file being used by |
| 36 | another process" error. |
| 37 | """ |
| 38 | tempdir = tempfile.gettempdir() |
| 39 | basename = 'tmpfile-%s' % (uuid.uuid4()) |
| 40 | full_filename = os.path.join(tempdir, basename) |
| 41 | if 'w' not in mode: |
| 42 | # We need to create the file before we can open |
| 43 | # it in 'r' mode. |
| 44 | open(full_filename, 'w').close() |
| 45 | try: |
| 46 | with open(full_filename, mode) as f: |
| 47 | yield f |
| 48 | finally: |
| 49 | os.remove(f.name) |
| 50 | |
| 51 | |
| 52 | class DataOnly(HTMLParser): |