python - 临时文件不可删除?

标签 python file-permissions delete-file

我已经使用 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)

错误

PermissionError: [WinError 32] The process cannot access the file
because it is being used by another process:
'C:\\Users\\MuhammadUsman\\Documents\\PythonScripts\\Project\\bingois3q1b3u.png'

最佳答案

试试这个:这对我有用。

import os
import tempfile

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

关于python - 临时文件不可删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50519942/

相关文章:

python - Numpy 数组减法

python - 从Python中的静态方法调用非静态方法

linux - 文件/目录权限尾随 + ( drwxr-xr-x+ )

linux - 使用 Apache 在 Redhat 上安装 Drupal 7 - 站点/默认/文件不可写

页面上的 Python 抓取仍然包含像\r\n\t 这样的字符

python - 如何在循环中制作 tkinter 框架并更新对象值?

file-permissions - 从用户空间 : operation not permitted 读取/dev/cpu/*/msr

file - 将 7 个最新文件保存在文件夹中的批处理文件

c - stdio 的 remove() 并不总是按时删除

ios - 从表中删除并删除文件