==tempfile 模块==

[Example 2-6 #eg-2-6] 中展示的 ``tempfile`` 模块允许你快速地创建名称唯一的临时文件供使用.

====Example 2-6. 使用 tempfile 模块创建临时文件====[eg-2-6]

```
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
``` ``TemporaryFile`` 函数会自动挑选合适的文件名, 并打开文件, 如 [Example 2-7 #eg-2-7] 所示.
而且它会确保该文件在关闭的时候会被删除. (在 Unix 下, 你可以删除一个已打开的文件, 这
时文件关闭时它会被自动删除. 在其他平台上, 这通过一个特殊的封装类实现.) ====Example 2-7. 使用 tempfile 模块打开临时文件====[eg-2-7] ```
File: tempfile-example-2.py import tempfile file = tempfile.TemporaryFile() for i in range(100):
file.write("*" * 100) file.close() # removes the file!
```

python标准库介绍——17 tempfile 模块详解的更多相关文章

  1. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  2. python标准库介绍——12 time 模块详解

    ==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...

  3. python标准库介绍——10 sys 模块详解

    ==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...

  4. python标准库介绍——30 code 模块详解

    ==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...

  5. python标准库介绍——8 operator 模块详解

    ==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...

  6. python标准库介绍——36 popen2 模块详解

    ==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...

  7. python标准库介绍——33 thread 模块详解

    ?==thread 模块== (可选) ``thread`` 模块提为线程提供了一个低级 (low_level) 的接口, 如 [Example 3-6 #eg-3-6] 所示. 只有你在编译解释器时 ...

  8. python标准库介绍——23 UserString 模块详解

    ==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...

  9. python标准库介绍——22 UserList 模块详解

    ==UserList 模块== ``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装). 在 [Example 2-16 #eg-2-16] 中, / ...

随机推荐

  1. xpath语法速查

    xpath的具体学习可以通过w3c查看(链接:http://www.w3school.com.cn/xpath/index.asp) 这里只是将平时用到的几个表格贴出来,以后查询: 这里的xpath我 ...

  2. WPF 同一个程序 只允许 同时运行一个

            方法2  当程序已经运行了 再运行这个程序时,则显示当前这个窗体  http://code.3rbang.com/cshape-run-one/ VS2013附件:http://fil ...

  3. iOS SEL类型和创建

    SEL selAction =NSSelectorFromString([actionArrayobjectAtIndex:indexArray]); [item addTarget:self act ...

  4. 分享几个.NET 下的计划任务组件

    Quartz http://www.quartz-scheduler.net/(现项目在使用,可以看我之前的文章) Hangfire http://hangfire.io/ Install-Packa ...

  5. 如何更改删除window服务?

    Cmd 下输入 sc delete 服务名 想要看服务名直接cmd下输入services.msc 打开服务, 例如删除apache服务 命令:sc delete Apache2.4

  6. My Magic Android Tour —— 处女作

    近期考试什么的都已经结束了,闲在家也没什么事做,就想着学点什么,于是便选择了学一下Android开发.一直在使用Android手机.要是自己能为自己的手机开发一个APP就好了. 好了,也不扯些废话了, ...

  7. Unity3D - 资源管理

    一直没有总结过Unity的资源管理,都是随用随看文档.今天有人问起,总结一下.加深对Unity资源管理的理解. 主要參考了Unity官方文档之Resources和AssetBundle. Unity有 ...

  8. http 事务

    #事务#HTTP权威指南 9页一个事务由一条请求命令和一个响应结果组成.这种通信是通过名叫HTTP报文(http message)的格式化数据块进行的

  9. java Map Set遍历

    Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法,keySet()方法返回值是Map中key值的集合:en ...

  10. topas解析(AIX)

    topas解析   topas 的显示信息和解析 (1) topas monitor for host:localhost  topas监控的主机名称localhost tue Aug 14 14:1 ...