我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用tempfile.template()。
def test_exports(self): # There are no surprising symbols in the tempfile module dict = tempfile.__dict__ expected = { "NamedTemporaryFile" : 1, "TemporaryFile" : 1, "mkstemp" : 1, "mkdtemp" : 1, "mktemp" : 1, "TMP_MAX" : 1, "gettempprefix" : 1, "gettempdir" : 1, "tempdir" : 1, "template" : 1, "SpooledTemporaryFile" : 1 } unexp = [] for key in dict: if key[0] != '_' and key not in expected: unexp.append(key) self.assertTrue(len(unexp) == 0, "unexpected keys: %s" % unexp)
def test_exports(self): # There are no surprising symbols in the tempfile module dict = tempfile.__dict__ expected = { "NamedTemporaryFile" : 1, "TemporaryFile" : 1, "mkstemp" : 1, "mkdtemp" : 1, "mktemp" : 1, "TMP_MAX" : 1, "gettempprefix" : 1, "gettempdir" : 1, "tempdir" : 1, "template" : 1, "SpooledTemporaryFile" : 1, "TemporaryDirectory" : 1, } unexp = [] for key in dict: if key[0] != '_' and key not in expected: unexp.append(key) self.assertTrue(len(unexp) == 0, "unexpected keys: %s" % unexp)
def __init__(self, suffix="", prefix=template, dir=None): self.name = mkdtemp(suffix, prefix, dir) self._closed = False
def make_temp(self): return tempfile._mkstemp_inner(tempfile.gettempdir(), tempfile.template, '', tempfile._bin_openflags)
def default_mkstemp_inner(self): return tempfile._mkstemp_inner(tempfile.gettempdir(), tempfile.template, '', tempfile._bin_openflags)
def make_temp_file(**kw): try: result = tempfile.mktemp(**kw) result = os.path.realpath(result) except TypeError: try: save_template = tempfile.template prefix = kw['prefix'] del kw['prefix'] tempfile.template = prefix result = tempfile.mktemp(**kw) finally: tempfile.template = save_template return result
def temppath(prefix=None, suffix=""): import tempfile temp = tempfile.NamedTemporaryFile(prefix=prefix if prefix is not None else tempfile.template, suffix=suffix, delete=False) try: temp.close() yield temp.name finally: os.remove(temp.name)
def __init__(self, suffix="", prefix=template, dir=None): self.name = mkdtemp(suffix, prefix, dir)
def remove_test_files(): """Remove files and directores created during tests.""" for name in os.listdir(u('.')): if name.startswith(tempfile.template): if os.path.isdir(name): shutil.rmtree(name) else: safe_remove(name)
def get_server_handler(): """Return the first FTPHandler instance running in the IOLoop.""" ioloop = IOLoop.instance() for fd in ioloop.socket_map: instance = ioloop.socket_map[fd] if isinstance(instance, FTPHandler): return instance raise RuntimeError("can't find any FTPHandler instance") # commented out as per bug http://bugs.python.org/issue10354 # tempfile.template = 'tmp-pyftpdlib'
def tempdir(self, path=None): """Creates a temporary directory. A unique directory name is generated if no path name is specified. The directory is created, and will be removed when the TestCmd object is destroyed. """ if path is None: try: path = tempfile.mktemp(prefix=tempfile.template) except TypeError: path = tempfile.mktemp() os.mkdir(path) # Symlinks in the path will report things # differently from os.getcwd(), so chdir there # and back to fetch the canonical path. cwd = os.getcwd() try: os.chdir(path) path = os.getcwd() finally: os.chdir(cwd) # Uppercase the drive letter since the case of drive # letters is pretty much random on win32: drive,rest = os.path.splitdrive(path) if drive: path = string.upper(drive) + rest # self._dirlist.append(path) global _Cleanup try: _Cleanup.index(self) except ValueError: _Cleanup.append(self) return path
def get_temp_file(self, prefix=template, suffix=""): """ On windows we cannot open temporary files after creating them unless we close them first. For this reason we must also create them with delete=False (or they would be deleted immediately when closed) and we want to make sure that the test object owns a reference to the file objects by adding them to self._tempfiles, so that they can be deleted when the test finishes. """ ret = NamedTemporaryFile(delete=False, prefix=prefix, suffix=suffix) self._tempfiles.append(ret) if is_win(): ret.close() return ret
def create_temp_file_name(suffix, prefix=None, dir=None): """small convinience function that creates temporal file. This function is a wrapper aroung Python built-in function - tempfile.mkstemp """ if not prefix: prefix = tempfile.template fd, name = tempfile.mkstemp( suffix=suffix, prefix=prefix, dir=dir ) file_obj = os.fdopen( fd ) file_obj.close() return name