def NamedTemporaryFile(mode='w+b', bufsize=-1,
suffix="tmp", prefix=gettempprefix(), dir=None, delete=True):
""" Variation on tempfile.NamedTemporaryFile(…), such that suffixes are passed
WITHOUT specifying the period in front (versus the standard library version
which makes you pass suffixes WITH the fucking period, ugh).
"""
from tempfile import _bin_openflags, _text_openflags, \
_mkstemp_inner, _os, \
_TemporaryFileWrapper, \
gettempdir
if dir is None:
dir = gettempdir()
if 'b' in mode:
flags = _bin_openflags
else:
flags = _text_openflags
if _os.name == 'nt' and delete:
flags |= _os.O_TEMPORARY
(fd, name) = _mkstemp_inner(dir, prefix, ".%s" % suffix, flags)
try:
file = _os.fdopen(fd, mode, bufsize)
return _TemporaryFileWrapper(file, name, delete)
except BaseException as baseexc:
_os.unlink(name)
_os.close(fd)
raise FilesystemError(str(baseexc))
评论列表
文章目录