您当前的位置: 首页 >  彭世瑜 Python

Python编程:tempfile创建临时文件

彭世瑜 发布时间:2018-11-14 19:54:47 ,浏览量:2

tempfile需要的时候创建零时文件,关闭之后就被删除了

import tempfile

import os

# 创建文件
file = tempfile.TemporaryFile(mode="w+")
print(file.name)
# 4

print(os.path.exists(file.name))
# True

# 写入、读取操作
file.write("hello world")

file.seek(0)
print(file.read())
# hello world

# 关闭资源
file.close()

print(os.path.exists(file.name))
# False

参考: https://docs.python.org/3/library/tempfile.html

关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 2浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0509s