==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. 【架构】Kubernetes和Spring Cloud哪个部署微服务更好?

    Spring Cloud 和Kubernetes都自称自己是部署和运行微服务的最好环境,但是它们在本质上和解决不同问题上是有很大差异的.在本文中,我们将看到每个平台如何帮助交付基于微服务的架构(MSA ...

  2. windows CMD命令查看局域网内所有主机名及IP

    COLOR 0A CLS @ECHOOff Title查询局域网内在线电脑IP :send @ECHO off&setlocal enabledelayedexpansion ECHO 正在获 ...

  3. OAuth 2.0 Authorization Framework RFC

    Internet Engineering Task Force (IETF) D. Hardt, Ed.Request for Comments: 6749 MicrosoftObsoletes: 5 ...

  4. 使用QQ截图右键菜单

    (1)登录QQ: (2)同时按住Shift+Ctrl+Alt+A四个键,然后点击右键: (3)松开Shift键(只松开Shift键,其他键不要松开).惊喜出现了吧!

  5. 该内存不能为written

    #include <stdio.h>int main(){ char *p = "abc"; printf("%s\n", p); *p = 'd' ...

  6. POSIX 线程详解

    一种支持内存共享的简捷工具 POSIX(可移植操作系统接口)线程是提高代码响应和性能的有力手段.在本系列中,Daniel Robbins 向您精确地展示在编程中如何使用线程.其中还涉及大量幕后细节,读 ...

  7. Linux高级权限管理

    传统的UGO(rwx-wx-wx)权限模型,无法解决当多个组需要对一个文件执行某些权限的问题. ACL :访问控制列表access control list一种高级的权限机制,允许我们对文件或者文件夹 ...

  8. html 语义化标签拾遗

    1.del和ins标签 兼容性:浏览器全部支持 del:定义文档中已被删除的文本. ins:定义已经被插入文档中的文本. <!DOCTYPE html> <html lang=&qu ...

  9. 寻找[nginx] 由Lua 粘合的Nginx生态环境-- agentzh

    来自:linuxtone org     Chnangelog:         120312 fixed as s/hhttp/http/g ,thanx muxueqz         12030 ...

  10. systemctl的常用命令

    #systemctl #systemctl --all #systemctl list-units --type=sokect #systemctl list-units --type=service ...