第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > python中tmp什么意思_python中temp是什么意思-问答-阿里云开发者社区-阿里云

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

时间:2024-04-21 02:10:41

相关推荐

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!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。