The tempfile module

The tempfile module

This module allows you to quickly come up with unique names to use for temporary files.

Example: Using the tempfile module to create filenames for temporary files
# File: tempfile-example-1.py

import tempfile
import os tempfile = tempfile.mktemp() print "tempfile", "=>", tempfile file = open(tempfile, "w+b")
file.write("*" * 1000)
file.seek(0)
print len(file.read()), "bytes"
file.close() try:
# must remove file when done
os.remove(tempfile)
except OSError:
pass
tempfile => C:\TEMP\~160-1
1000 bytes

The TemporaryFile function picks a suitable name, and opens the file. It also makes sure that the file is removed when it’s closed (under Unix, you can remove an open file and have it disappear when the file is closed. On other platforms, this is done via a special wrapper class).

Example: Using the tempfile module to open temporary files
# File: tempfile-example-2.py

import tempfile

file = tempfile.TemporaryFile()

for i in range(100):
file.write("*" * 100) file.close() # removes the file!

The tempfile module的更多相关文章

  1. Modules you should know in Python Libray

    前两天被问到常用的python lib和module有哪些?最常用的那几个,其他的一下子竟然回答不上.想想也是,一般情况下,遇到一个问题,在网上一搜,顺着线索找到可用的例子,然后基本没有怎么深究.结果 ...

  2. import os, glob, fnmatch--Python os/glob/fnmatch主要函数总结

    auther: Lart date: 2019-01-17 update: 2019-01-18 09:55:36 --- import os, glob, fnmatch 针对某些操作, 官方推荐这 ...

  3. Python中sys和os模块的区别

    sys: This module provides access to some variables used or maintained by the interpreter and to func ...

  4. python文档自译:os模块-01【说明部分】

    15.1. os - Miscellaneous operating system interfaces This module provides a portable way of using op ...

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

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

  6. TempFile模块

    tempfile模块,用来对临时数据进行操作 tempfile 临时文件(夹)操作 tempfile.mkstemp([suffix="[, prefix='tmp'[, dir=None[ ...

  7. 关于python 文件操作os.fdopen(), os.close(), tempfile.mkstemp()

    嗯.最近在弄的东西也跟这个有关系,由于c基础渣渣.现在基本上都忘记得差不多的情况下,是需要花点功夫才能弄明白. 每个语言都有相关的文件操作. 今天在flask 的例子里看到这样一句话.拉开了文件操作折 ...

  8. 解决AttributeError: 'module' object has no attribute 'main' 安装第三方包报错

    1.找到pycharm 目录下的 \helper\packaging_tool.py 文件 2.用新版pycharm 的packaging_tool.py 替换 旧版 同名文件 文件代码如下: imp ...

  9. Android Studio 编译单个module

    前期自己要把gradle环境变量配置好 在Terminal中gradle命令行编译apk 输入gradle assembleRelease 会编译全部module编译单个modulecd ./xiru ...

随机推荐

  1. 在Ubuntu下编译Assimp库

    在Ubuntu下编译Assimp库 怎样在Ubuntu下编译Assimp库?这是我曾经编译成功后做的笔记,供參考. 1.去以下的站点去下载Assimp库: http://assimp.sourcefo ...

  2. linux环境: shell初始化文件, for TCSH, CSH

    TCSHELL, CSHELL 配置文件 全局配置文件 /etc/csh.cshrc个人配置文件 ~/.cshrc或~/.tcshrc 参考: 1.配置你的csh/tcsh,  https://wik ...

  3. 测试markdown语法

    测试使用markdown 这是无序列表 空调 洗衣机 电脑 这是有序列表 西瓜 哈密瓜 火龙果 下划线bingo 测试 斜体好丑 粗体很赞 测试插入代码 $(document).ready(funct ...

  4. css盒模型和块级、行内元素深入理解

    盒模型是CSS的核心知识点之一,它指定元素如何显示以及如何相互交互.页面上的每个元素都被看成一个矩形框,这个框由元素的内容.内边距.边框和外边距组成,需要了解的朋友可以深入参考下 一.CSS盒模型 盒 ...

  5. zoj1028-Flip and Shift

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=28 题意:有相互交叉的黑白两种颜色的小球,每一个小球每次可以跳两格:问你是否可以 ...

  6. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

    题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...

  7. Selenium WebDriver java 简单实例

    开发环境 JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse: 下载地址:http ...

  8. 让Delphi XE2程序支持UAC

    在win7下,开发的程序有的时候莫名其妙就不能正常工作了,其实都是因为权限不够,要想能够正常运行,就需要获得管理员权限,这就需要处理UAC.具体方法如下: 一,制作“uac.manifest”文件.新 ...

  9. 获取合并单元格中值的一个方法POI

    private static String getCellValueForMerginRegion(Cell cell) { int rowIdx=cell.getRowIndex(); Sheet ...

  10. 11661 - Burger Time?

      Burger Time?  Everybody knows that along the more important highways there are countless fast food ...