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. 在JS中,一个自定义函数如何调用另一个自定义函数中的变量

    function aa1511() { var chengshi="马鞍山"; var shengfen="安徽省"; return shengfen+&quo ...

  2. Winform - 判断GroupBox控件中的TextBox文本框是不是为空

    foreach (Control item in this.groupBox2.Controls) { if (item is TextBox) { if (item.Text.Trim() == & ...

  3. js操作styleSheets

    document.styleSheets这个接口可以获取网页上引入的link样式表和style样式表.比如 最后的输出结果如下. 换下代码看看我们具体的styleSheets具体输出什么 这些都是次要 ...

  4. 学习老外用webstorm开发nodejs的技巧--代码提示DefinitelyTyped

    最近入了nodejs的坑,作为老码农,js对我来说还是很容易的.webstorm虽说用得不多,但是pycharms我是老手了,idea的东西一脉相承,想想也就那样了. 但是自从看了某个视频后,觉得毕竟 ...

  5. Unknown database 'DB_NAME'

    Cannot create PoolableConnectionFactory (Unknown database 'DB_NAME'): com.mysql.jdbc.exceptions.jdbc ...

  6. linux下C/C++IDE比较——Code::Blocks

    工欲善其事,必先利其器.用了这么久的linux,现在比较主流的几个C/C++的IDE基本已都用过了,现在来对他们做一下简单的比较. 1.VIM首先要说的是VIM.我认为,VIM只是一个编辑器,不能算是 ...

  7. Boost::thread库的使用

    阅读对象 本文假设读者有几下Skills [1]在C++中至少使用过一种多线程开发库,有Mutex和Lock的概念. [2]熟悉C++开发,在开发工具中,能够编译.设置boost::thread库. ...

  8. Java中@Deprecated、@SupressWarning、@Override的作用

    Annotation注解在 Java 中有着很广泛的,他是做为一种标识 为javac所识别.每一个注解 都对应这一个Java类  在java.lang包中 有三个注解  分别是 Deprecated  ...

  9. 每日一帖示例程序(使用TWebBrowser基于HTML做)

    最近在程序中增加了每日一帖的功能,搜索一下网站的程序,发现大部分是用Memo实现,而我用的是TWebBrowser基于HTML做,故帖出来共享一下. PAS源码: unit Unit1; interf ...

  10. perl .*?和.*

    redis01:/root# cat x2.pl my $str="212121a19823a456123"; if ($str =~/.*a(.*)23/){print &quo ...