python - 如何在临时目录中写入和读取 txt 文件?
问题描述
我想要一个临时目录和一个 txt 文件,一旦临时目录关闭,该文件将被删除。但是当我尝试编写一个 txt 文件(在临时目录中创建)时,没有写入 txt 文件,其中没有内容。
例子:
import tempfile
message = 'this is my message'
with tempfile.TemporaryDirectory(dir='../', prefix='temp_') as td:
td = td.replace(chr(92), '/')
with open(f'{td}/filename.txt', 'w+') as temporary_file:
temporary_file.write(message)
temporary_file.read()
# do some stuff here
# the temporary directory was closed, removing the txt file.
解决方案
推荐阅读
- selenium - 在使用 java 的 Selenium Webdriver 中,热点击“获取 adobe Flash Player”图标?
- wordpress - VPS 上 WordPress 的权限
- c - 在C编程中实现堆栈的push方法
- selenium - 我们可以使用appium android阅读移动设备上可用的pdf文件吗
- javascript - 使用电子从文件系统中选择并显示图像
- php - 使用 URL 将参数传递给函数
- hyperledger-fabric - 如何从 Hyperledger 结构块生成数据透视表数据(汇总数据)
- ios - Xcode 10资产目录保留矢量数据不起作用
- android - 如何为 3 种不同状态设置按钮样式
- javascript -
状态已更新但也不重新渲染