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. Win7+花生壳6.0+tomcat打做自己的web服务器(搭建自己的网站)(参考)

    链接地址:http://blog.csdn.net/zhu_9527/article/details/23344623?utm_source=tuicool&utm_medium=referr ...

  2. C#获取千分位,给数字加逗号分隔符

    /// <summary> /// 对数字添加”,“号,可以处理负数以及带有小数的情况 /// </summary> /// <param name="vers ...

  3. javascript笔记整理(数据类型强制/隐式转换 )

    A.数据类型强制转换 1.转换为数值类型 Number(参数) 把任何的类型转换为数值类型 A.如果是布尔值,false为0,true为1 var a=false;alert(Number(a)); ...

  4. Linux下安装配置词典GoldenDict

    GoldenDict   apt-get install goldendict 安装完成以后,需要自己手动加入字典,增加在线翻译网址(例如,有道,金山等),点击 编辑--dictionaries  完 ...

  5. 调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment之我的解决方法

    private class ViewPagerAdapter extends FragmentPagerAdapter {      FragmentManager mFragmentManager; ...

  6. java假设模拟请求重新启动路由器(网络爬虫经常使用),还有java怎样下载图片

    我们假设在公司或家里使用网络爬虫去抓取自己索要的一些数据的时候,经常对方的站点有defence机制,会给你的http请求返回500错误,仅仅要是同样IP就请求不到数据,这时候我们仅仅能去重新启动路由器 ...

  7. 解 自己关于 C# Button的Click事件的疑惑

    先说说C#中事件的用法,从事件的用法中,我自己会产生一个疑惑 C#事件 class Program { static void Main(string[] args) { EventClass ec ...

  8. eclipse升级后Android使用JAR报错

    升级ADT22以后,老项目编译时后遇到 NoDefFoundClassError  这个错误,因为项目中使用了jar文件. 遇到此问题的解决步骤: 1.项目根目录下建立 libs ,并将jar文件移入 ...

  9. linux下nginx负载均衡部署

    nginx负载均衡部署 Nginx("engine x") 是一个高性能的 HTTP 和 反向代理 server,也是一个 IMAP/POP3/SMTP 代理server. Ngi ...

  10. Wireshark入门与进阶---数据包捕获与保存的最基本流程

    Wireshark入门与进阶系列(一) "君子生非异也.善假于物也"---荀子 本文由CSDN-蚍蜉撼青松 [主页:http://blog.csdn.net/howeverpf]原 ...