zoukankan      html  css  js  c++  java
  • TempFile模块

    tempfile模块,用来对临时数据进行操作

    tempfile 临时文件(夹)操作

    tempfile.mkstemp([suffix=”[, prefix=’tmp'[, dir=None[, text=False]]]])

    mkstemp方法用于创建一个临时文件。该方法仅仅用于创建临时文件,调用tempfile.mkstemp函数后,返回包含两个元素的元组,第一个元素指示操作该临时文件的安全级别,第二个元素指示该临时文件的路径。参数suffix和prefix分别表示临时文件名称的后缀和前缀;dir指定了临时文件所在的目录,如果没有指定目录,将根据系统环境变量TMPDIR, TEMP或者TMP的设置来保存临时文件;参数text指定了是否以文本的形式来操作文件,默认为False,表示以二进制的形式来操作文件。

    tempfile.mkdtemp([suffix=”[, prefix=’tmp'[, dir=None]]])

    该函数用于创建一个临时文件夹。参数的意思与tempfile.mkdtemp一样。它返回临时文件夹的绝对路径。

    tempfile.mktemp([suffix=”[, prefix=’tmp'[, dir=None]]])

    mktemp用于返回一个临时文件的路径,但并不创建该临时文件。

    tempfile.tempdir

    该属性用于指定创建的临时文件(夹)所在的默认文件夹。如果没有设置该属性或者将其设为None,Python将返回以下环境变量TMPDIR, TEMP, TEMP指定的目录,如果没有定义这些环境变量,临时文件将被创建在当前工作目录。

    tempfile.gettempdir()

    gettempdir()则用于返回保存临时文件的文件夹路径。

    tempfile.TemporaryFile([mode=’w+b'[, bufsize=-1[, suffix=”[, prefix=’tmp'[, dir=None]]]]])

    该函数返回一个类文件对象(file-like)用于临时数据保存(实际上对应磁盘上的一个临时文件)。当文件对象被close或者被del的时候,临时文件将从磁盘上删除。mode、bufsize参数的单方与open()函数一样;suffix和prefix指定了临时文件名的后缀和前缀;dir用于设置临时文件默认的保存路径。返回的类文件对象有一个file属性,它指向真正操作的底层的file对象。

    tempfile.NamedTemporaryFile([mode=’w+b'[, bufsize=-1[, suffix=”[, prefix=’tmp'[, dir=None[, delete=True]]]]]])

    tempfile.NamedTemporaryFile函数的行为与tempfile.TemporaryFile类似,只不过它多了一个delete参数,用于指定类文件对象close或者被del之后,是否也一同删除磁盘上的临时文件(当delete = True的时候,行为与TemporaryFile一样)。

    tempfile.SpooledTemporaryFile([max_size=0[, mode=’w+b'[, bufsize=-1[, suffix=”[, prefix=’tmp'[, dir=None]]]]]])

    tempfile.SpooledTemporaryFile函数的行为与tempfile.TemporaryFile类似。不同的是向类文件对象写数据的时候,数据长度只有到达参数max_size指定大小时,或者调用类文件对象的fileno()方法,数据才会真正写入到磁盘的临时文件中。


    官方用法

    • DESCRIPTION
      This module provides generic, low- and high-level interfaces for
      creating temporary files and directories. All of the interfaces
      provided by this module can be used without fear of race conditions
      except for 'mktemp'. 'mktemp' is subject to race conditions and
      should not be used; it is provided for backward compatibility only.

      This module also provides some data items to the user:

      TMP_MAX  - maximum number of names that will be tried before
                 giving up.
      template - the default prefix for all temporary names.
                 You may change this to control the default prefix.
      tempdir  - If this is set to a string before the first use of
                 any routine from this module, it will be considered as
                 another candidate location to store temporary files.
      
    • CLASSES
      SpooledTemporaryFile

      class SpooledTemporaryFile
      | Temporary file wrapper, specialized to switch from
      | StringIO to a real file when it exceeds a certain size or
      | when a fileno is needed.
      |
      | Methods defined here:
      |
      | enter(self)
      | # Context management protocol
      |
      | exit(self, exc, value, tb)
      |
      | init(self, max_size=0, mode='w+b', bufsize=-1, suffix='', prefix='tmp', dir=None)
      |
      | iter(self)
      | # file protocol
      |
      | close(self)
      |
      | fileno(self)
      |
      | flush(self)
      |
      | isatty(self)
      |
      | next(self)
      |
      | read(self, *args)
      |
      | readline(self, *args)
      |
      | readlines(self, *args)
      |
      | rollover(self)
      |
      | seek(self, *args)
      |
      | tell(self)
      |
      | truncate(self)
      |
      | write(self, s)
      |
      | writelines(self, iterable)
      |
      | xreadlines(self, *args)
      |
      | ----------------------------------------------------------------------
      | Data descriptors defined here:
      |
      | closed
      |
      | mode
      |
      | name
      |
      | softspace

    • FUNCTIONS
      NamedTemporaryFile(mode='w+b', bufsize=-1, suffix='', prefix='tmp', dir=None, delete=True)
      Create and return a temporary file.
      Arguments:
      'prefix', 'suffix', 'dir' -- as for mkstemp.
      'mode' -- the mode argument to os.fdopen (default "w+b").
      'bufsize' -- the buffer size argument to os.fdopen (default -1).
      'delete' -- whether the file is deleted on close (default True).
      The file is created as mkstemp() would do it.

        Returns an object with a file-like interface; the name of the file
        is accessible as its 'name' attribute.  The file will be automatically
        deleted when it is closed unless the 'delete' argument is set to False.
      

      TemporaryFile = NamedTemporaryFile(mode='w+b', bufsize=-1, suffix='', prefix='tmp', dir=None, delete=True)
      Create and return a temporary file.
      Arguments:
      'prefix', 'suffix', 'dir' -- as for mkstemp.
      'mode' -- the mode argument to os.fdopen (default "w+b").
      'bufsize' -- the buffer size argument to os.fdopen (default -1).
      'delete' -- whether the file is deleted on close (default True).
      The file is created as mkstemp() would do it.

        Returns an object with a file-like interface; the name of the file
        is accessible as its 'name' attribute.  The file will be automatically
        deleted when it is closed unless the 'delete' argument is set to False.
      

      gettempdir()
      Accessor for tempfile.tempdir.

      gettempprefix()
      Accessor for tempdir.template.

      mkdtemp(suffix='', prefix='tmp', dir=None)
      User-callable function to create and return a unique temporary
      directory. The return value is the pathname of the directory.

        Arguments are as for mkstemp, except that the 'text' argument is
        not accepted.
        
        The directory is readable, writable, and searchable only by the
        creating user.
        
        Caller is responsible for deleting the directory when done with it.
      

      mkstemp(suffix='', prefix='tmp', dir=None, text=False)
      User-callable function to create and return a unique temporary
      file. The return value is a pair (fd, name) where fd is the
      file descriptor returned by os.open, and name is the filename.

        If 'suffix' is specified, the file name will end with that suffix,
        otherwise there will be no suffix.
        
        If 'prefix' is specified, the file name will begin with that prefix,
        otherwise a default prefix is used.
        
        If 'dir' is specified, the file will be created in that directory,
        otherwise a default directory is used.
        
        If 'text' is specified and true, the file is opened in text
        mode.  Else (the default) the file is opened in binary mode.  On
        some operating systems, this makes no difference.
        
        The file is readable and writable only by the creating user ID.
        If the operating system uses permission bits to indicate whether a
        file is executable, the file is executable by no one. The file
        descriptor is not inherited by children of this process.
        
        Caller is responsible for deleting the file when done with it.
      

      mktemp(suffix='', prefix='tmp', dir=None)
      User-callable function to return a unique temporary file name. The
      file is not created.

        Arguments are as for mkstemp, except that the 'text' argument is
        not accepted.
        
        This function is unsafe and should not be used.  The file name
        refers to a file that did not exist at some point, but by the time
        you get around to creating it, someone else may have beaten you to
        the punch.
      
    • DATA
      TMP_MAX = 32767
      all = ['NamedTemporaryFile', 'TemporaryFile', 'SpooledTemporaryFil...
      tempdir = None

  • 相关阅读:
    Bzoj4872: [Shoi2017]分手是祝愿
    大数据应用价值研究员--数据可视化工程师
    Angular Redux
    Reactive Redux
    Testing a Redux & React web application
    [转]于Fragment和Activity之间onCreateOptionsMenu的问题
    [转]探究java IO之FileInputStream类
    深入解析FileInputStream和FileOutputStream
    [转]慎用InputStream的read()方法
    [转]Android
  • 原文地址:https://www.cnblogs.com/zhy128/p/8046420.html
Copyright © 2011-2022 走看看