MCPcopy
hub / github.com/awslabs/aws-shell / temporary_file

Function temporary_file

awsshell/utils.py:31–49  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

29
30@contextlib.contextmanager
31def 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
52class DataOnly(HTMLParser):

Callers 4

runMethod · 0.90
test_can_open_in_readMethod · 0.90

Calls 1

closeMethod · 0.80