python中tmp什么意思_python中temp是什么意思-问答-阿里云开发者社区-阿里云

==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

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值