更新:我在线托管时没有收到错误。可以正常删除。保留这个以防万一有任何在 Windows 电脑上本地运行的有用答案。
上下文:我从discord下载了一个pdf文件。然后我读了它等等,然后我删除了它。 (我必须首先下载并删除,因为直接从不和谐对象读取它导致我达到不和谐的速率限制)
到目前为止我已经尝试过的事情
每次都出现同样的错误 WindowsError:[错误 32] 该进程无法访问该文件,因为该文件正在被另一个进程使用:
在一些不和谐消息中,我传递了多个 pdf 文件。它在删除第一个时不会抛出错误,但在最后一个时又会抛出错误。
这是我的代码
filepath=os.path.join('downloaded_files', attachment.filename)
print(filepath)
with open(filepath, 'wb') as f:
f.write(content)
input_text=pdf_text_from_attachment(filepath)
to_send=input_text
await send_message_in_thread(
channel_id,
to_send)
f.close()
os.remove(filepath)
这应该有效:
filepath=os.path.join('downloaded_files', attachment.filename)
print(filepath)
with open(filepath, 'wb') as f:
f.write(content)
input_text=pdf_text_from_attachment(filepath)
await send_message_in_thread(channel_id, input_text)
os.remove(filepath)