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 ...
随机推荐
- “/” 应用程序中的服务器错误 - IIS 发布错误
解决方法, 将bin目录下的全部文件复制到iis下
- MongoDB使用手册
一.安装包下载地址 http://www.mongodb.org/downloads 二.服务器端使用 LINUX版本: ①用tar -zxvf mongodb-linux-x86_64-1.2.4. ...
- 基于visual Studio2013解决C语言竞赛题之1050矩阵反斜线求和
题目 解决代码及点评 /************************************************************************/ /* 50 ...
- hihocoder1302 最长回文子串
hihocoder1302 最长回文子串 先贴代码 所有的上面的提示已经交代的好清楚了…… #include <iostream> #include <cstring> #in ...
- Android——内存调试
因调试某个重大问题,怀疑到了内存,专门写了个測试脚本.记录一下. 撰写不易,转载请注明出处:http://blog.csdn.net/jscese/article/details/37928823 一 ...
- VB.net总结
.NET视频差点儿相同用时一周结束,总体感觉就是"走耳不走脑".刚開始看视频理解起来有一点困难,台湾口音以及台湾与大陆计算机术语的差异,让我把前几集相当于直接忽略过了(建议打算看这 ...
- 细节!重点!易错点!--面试java基础篇(二)
今天来给大家分享一下java的重点易错点第二部分,也是各位同学面试需要准备的,欢迎大家交流指正. 1.字符串创建与存储机制:当创建一个字符串时,首先会在常量池中查找是否已经有相同的字符串被定义,其判断 ...
- 基于visual Studio2013解决C语言竞赛题之1078打印日历
题目 解决代码及点评 /************************************************************************/ /* ...
- span设置固定宽度
<span> 标签是被用来组合文档中的行内元素.相信对一般的网页设计师来讲是非常熟悉的朋友了,使用相当频繁,但我们往往很少对SPAN设定样式,一般也没什么必要,大多数都留给DIV老朋友了. ...
- 后台调用外部程序的完美实现(使用CreateDesktop建立隐藏桌面)
最近在做的一个软件,其中有一部分功能需要调用其它的软件来完成,而那个软件只有可执行文件,根本没有源代码,幸好,我要做的事不难,只需要在我的程序启动后,将那个软件打开,在需要的时候,对其中的一个文本矿设 ...