Skip to content

Instantly share code, notes, and snippets.

@wonderbeyond
Created July 17, 2023 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wonderbeyond/6dcf8bcfe2671ef72c36cf2ff75e3307 to your computer and use it in GitHub Desktop.
Save wonderbeyond/6dcf8bcfe2671ef72c36cf2ff75e3307 to your computer and use it in GitHub Desktop.
An alternative to tempfile.NamedTemporaryFile working with Windows NT.
import os
from contextlib import contextmanager
from tempfile import mkstemp
@contextmanager
def MakeTemporaryFile(suffix=None, prefix=None, dir=None):
"""An alternative to tempfile.NamedTemporaryFile working with Windows NT."""
try:
fd, name = mkstemp(suffix=suffix, prefix=prefix, dir=dir)
yield name
finally:
os.close(fd)
os.remove(name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment