gpt4 book ai didi

python - 无法访问使用 tempfile 创建的临时文件

转载 作者:可可西里 更新时间:2023-11-01 13:57:58 26 4
gpt4 key购买 nike

我正在使用 tempfile.NamedTemporaryFile()存储一些文本直到程序结束。在 Unix 上工作没有任何问题,但在 Windows 上返回的文件无法读取或写入:python 给出 Errno 13。唯一的方法是设置 delete=False并使用 os.remove() 手动删除文件.为什么?

最佳答案

This causes the IOError because the file can be opened only once after it is created.

原因是因为 NamedTemporaryFile 在 Windows 上创建了带有 FILE_SHARE_DELETE 标志的文件。在 Windows 上,当使用特定共享标志创建/打开文件时,所有后续打开操作都必须传递此共享标志。 Python 的 open 函数不是这种情况,它不传递 FILE_SHARE_DELETE 标志。在 How to create a temporary file that can be read by a subprocess? 上查看我的回答有关详细信息和解决方法的问题。

关于python - 无法访问使用 tempfile 创建的临时文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15588314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com