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:
passtempfile => C:\TEMP\~160-1
1000 bytesThe 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的更多相关文章
- Modules you should know in Python Libray
前两天被问到常用的python lib和module有哪些?最常用的那几个,其他的一下子竟然回答不上.想想也是,一般情况下,遇到一个问题,在网上一搜,顺着线索找到可用的例子,然后基本没有怎么深究.结果 ...
- 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 针对某些操作, 官方推荐这 ...
- Python中sys和os模块的区别
sys: This module provides access to some variables used or maintained by the interpreter and to func ...
- python文档自译:os模块-01【说明部分】
15.1. os - Miscellaneous operating system interfaces This module provides a portable way of using op ...
- [python] 创建临时文件-tempfile模块
This module generates temporary files and directories. It works on all supported platforms.In versio ...
- TempFile模块
tempfile模块,用来对临时数据进行操作 tempfile 临时文件(夹)操作 tempfile.mkstemp([suffix="[, prefix='tmp'[, dir=None[ ...
- 关于python 文件操作os.fdopen(), os.close(), tempfile.mkstemp()
嗯.最近在弄的东西也跟这个有关系,由于c基础渣渣.现在基本上都忘记得差不多的情况下,是需要花点功夫才能弄明白. 每个语言都有相关的文件操作. 今天在flask 的例子里看到这样一句话.拉开了文件操作折 ...
- 解决AttributeError: 'module' object has no attribute 'main' 安装第三方包报错
1.找到pycharm 目录下的 \helper\packaging_tool.py 文件 2.用新版pycharm 的packaging_tool.py 替换 旧版 同名文件 文件代码如下: imp ...
- Android Studio 编译单个module
前期自己要把gradle环境变量配置好 在Terminal中gradle命令行编译apk 输入gradle assembleRelease 会编译全部module编译单个modulecd ./xiru ...
随机推荐
- string和byte[]的转换 (C#)
原文 string和byte[]的转换 (C#) string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ...
- HTML5的优缺点是什么?
HTML5的优缺点是什么?作为HTML的第五次重大修改,HTML5有哪些改进?HTML5又有哪些缺点? 网络标准 HTML5本身是由W3C推荐出来的,它的开发是通过谷歌.苹果,诺基亚.中国移动等几百家 ...
- Stack栈的三种含义
理解stack栈对于理解程序的执行至关重要.easy混淆的是,这个词事实上有三种含义,适用于不同的场合,必须加以区分. 含义一:数据结构 stack的第一种含义是一组数据的存放方式,特点为LIFO,即 ...
- [置顶] Android下实现自动关机的方法总结
最近在网上看了一些Android下实现自动关机的方法,有的不行,有的只适用一些机型,有的适用于大部分机型,笔者在此总结一下 法一: Intent newIntent = new Intent(Inte ...
- asp.net2.0安全性(3)--验证与授权--转载来自车老师
"验证"与"授权"是对网页资源安全管理的两道门. 验证(Authentication):检查用户是否是合法的用户.就像是网站大门口的保卫,服责验证使用的用户名和 ...
- EasyUI - DateBox组件
效果: html代码: <input type ="text" id ="box" /> JS代码: $(function () { //设置返回格 ...
- java.exe路径问题
因为要更换JDK版本,自然也就要重新设置JAVA_HOME环境变量,但设置完成后奇怪的发现,运行java -version时还是原来的版本,莫名其妙,最后我把JAVA_HOME环境变量删除竟然java ...
- windows 2003 DNS服务的重建
DNS的重建 网络上转载之文章,据说来自微软官方,留下一份自己用,曾经帮助我解决了DNS方面的困扰,希望能给大家带来帮助 1.打开dns管理器,删除domain.com区域,如果存在_msdcs.do ...
- SIMPASS技术解析
一.什么叫SIMPASS SIMpass技术融合了DI卡技术和SIM卡技术,或者称为双界面SIM卡.SIMpass是一种多功能的SIM卡,支持接触与非接触两个工作接口,接触界面实现SIM功能,非接触界 ...
- SpringMVC经典系列-12基于SpringMVC的文件上传---【LinusZhu】
注意:此文章是个人原创.希望有转载须要的朋友们标明文章出处,假设各位朋友们认为写的还好,就给个赞哈,你的鼓舞是我创作的最大动力.LinusZhu在此表示十分感谢,当然文章中如有纰漏.请联系linusz ...