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

TempFile模块的更多相关文章

  1. pig脚本不需要后缀名(python tempfile模块生成pig脚本临时文件,执行)

    pig 脚本运行不需要后缀名 pig脚本名为tempfile,无后缀名 用pig -f tempfile 可直接运行 另外,pig tempfile也可以直接运行 这样就可以用python临时文件存储 ...

  2. python标准库介绍——17 tempfile 模块详解

    ==tempfile 模块== [Example 2-6 #eg-2-6] 中展示的 ``tempfile`` 模块允许你快速地创建名称唯一的临时文件供使用. ====Example 2-6. 使用 ...

  3. 【Python】 tempfile模块 临时文件和目录的处理

    [tempfile] 惊奇地又发现了一个比较有意思的小模块. 在一些场景中我们经常需要自动生成一些临时文件,当然用简单的open函数,来创建一个隐藏文件可以实现.不过tempfile这个模块把一些有的 ...

  4. [python] 创建临时文件-tempfile模块

    This module generates temporary files and directories. It works on all supported platforms.In versio ...

  5. python 各模块

    01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...

  6. Python之文件与目录操作及压缩模块(os、shutil、zipfile、tarfile)

    Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读取或写入 os.path模块 文件路径操作 os模块 文件和目录简单操作 zipfile模 ...

  7. python模块-OS模块详解

    1.按字母分 os相关的函数:143个.按字母排序如下: ['abort', 'access', 'altsep', 'chdir', 'chmod', 'chown', 'chroot', 'clo ...

  8. Python模块操作

    Exceptions 模块 该模块定义了以下标准异常: • Exception 是所有异常的基类. 强烈建议(但不是必须)自定义的异常异常也继承这个类. • SystemExit(Exception) ...

  9. Django----认证系统和auth模块

    COOKIE 与 SESSION 概念 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cookie ...

随机推荐

  1. Can you solve this equation?

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...

  2. gulp learning note

    为啥写这一片文章呢? 主要是为了温故而知新和分享,也是为了更加促进自己的学习! 前端自动化工具很多  有grunt  gulp  webpack 等 这次主要分享下gulp的学习经验,让自己更好的总结 ...

  3. VS2013装扩展RazorGenerator

    问题:vs2013工具扩展和更新搜索关键字:Razor Generator查找不到,如图 解决:下载网址:https://github.com/RazorGenerator/RazorGenerato ...

  4. Node.js 回调函数

    Node.js 回调函数 Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了. 回调函数在完成任务后就会被调用,Node 使用了大量的回调函数, ...

  5. python链接mysql以及常用语法

    MySQL是一个关系型数据库管理系统 ,其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择 MySQL 作为网站数据库.在使用过程中不总是和它打交道,导致使用时候都得 ...

  6. Thinkphp5 模型 验证器执行顺序问题

    Thinkphp5把模型的验证规则归为一个验证器,这种做法,不知到符不符合大家的心意,反正楼主是比较不爽的 楼主更倾向于tp3.2的验证规则直接写在模型里面,毕竟你的验证规则一般而言是针对模型来验证的 ...

  7. 浅谈postgresql的GIN索引(通用倒排索引)

    1.倒排索引原理 倒排索引来源于搜索引擎的技术,可以说是搜索引擎的基石.正是有了倒排索引技术,搜索引擎才能有效率的进行数据库查找.删除等操作.在详细说明倒排索引之前,我们说一下与之相关的正排索引并与之 ...

  8. 最近跟mysql的自动shutdown干上了。。。

    数据库变大之后,又开始不定时shutdown了. 搜了半天,也没啥用, 根据谋篇写的内容,改了下一个参数   vi /etc/my.cnf 修改innodb_buffer_pool_size = 16 ...

  9. scala读取parquet文件

    import org.apache.spark.SparkConfimport org.apache.spark.SparkContextimport org.apache.spark.sql.SQL ...

  10. sql2012笔记

    收缩数据库日志文件1.数据库右键-->Options-->Revovery model =Full 改成 Simple2.数据库右键-->Tasks-->Shrink--> ...