Python PyPDF2 合并不允许 os.remove 工作

Python PyPDF2 merger doesn't let os.remove work

我不知道如何关闭我试图在 python 中删除的文件。代码:

merger = PdfFileMerger()
for auth in newAuths:
    merger.append(auth)
merger.write(r"C:\stack\overflow.pdf")
for i in newAuths:
    os.remove(i)

一切正常,直到最后两行代码。它returns:'PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:' 想知道如何通过访问文件关闭合并吗?

该文件仍处于打开状态并由 merger 使用,PdfFileMerger()close() 方法将关闭该文件并允许您将其删除。

merger = PdfFileMerger()
for auth in newAuths:
    merger.append(auth)
merger.write(r"C:\stack\overflow.pdf")
merger.close()
for i in newAuths:
    os.remove(i)