临时文件不可删除?

2024-10-01 09:33:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经使用tempfile.mkstemp()创建了临时文件,创建之后,我得到了path中文件的唯一路径,现在我想删除这个临时文件。我的代码如下所示。在

我已经访问过这个WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'new.dat' ,但没有解决我的问题。在

编码

import os
import tempfile

path=tempfile.mkstemp('.png', 'bingo',
    'C:\\Users\\MuhammadUsman\\Documents\\PythonScripts\\Project')
os.unlink(path)

错误

^{pr2}$

Tags: 文件thepath代码import路径accessos
2条回答

如果你只想得到唯一的名字,那么试试这个。这比上面的解决方案好。不需要删除该文件。文件将自动删除。在

import os
import tempfile

path=tempfile.NamedTemporaryFile(dir='C:\\Users\\MuhammadUsman\\Documents\\Python Scripts\\Project',suffix='.png').name

试试这个:这个对我有用。在

import os
import tempfile

fd,path=tempfile.mkstemp('.png', 'bingo', 'C:\\Users\\MuhammadUsman\\Documents\\Python Scripts\\Project')
os.close(fd)
os.unlink(path)

相关问题 更多 >