临时文件不可删除?
Temporary file not deletable?
我已经使用 tempfile.mkstemp()
创建了临时文件,创建后,我在 path
中获得了文件的唯一路径,现在我想删除临时文件。我的代码如下。
我已经访问过这个Windows错误:[错误32]进程无法访问该文件,因为它正被另一个进程使用:'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)
如果您只想获得唯一的名称,那么试试这个。这比上面的解决方案更好。无需删除文件。文件将被自动删除。
import os
import tempfile
path=tempfile.NamedTemporaryFile(dir='C:\Users\MuhammadUsman\Documents\Python Scripts\Project',suffix='.png').name
我已经使用 tempfile.mkstemp()
创建了临时文件,创建后,我在 path
中获得了文件的唯一路径,现在我想删除临时文件。我的代码如下。
我已经访问过这个Windows错误:[错误32]进程无法访问该文件,因为它正被另一个进程使用:'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)
如果您只想获得唯一的名称,那么试试这个。这比上面的解决方案更好。无需删除文件。文件将被自动删除。
import os
import tempfile
path=tempfile.NamedTemporaryFile(dir='C:\Users\MuhammadUsman\Documents\Python Scripts\Project',suffix='.png').name