我正在尝试用python编写一个程序,该程序接受一个PDF文件并首先将任何包含水果名称(芒果,橙子或苹果)的pdf附加到它,然后将带有动物名称的pdf附加到原始文件(斑马、猴子、狗),最后附加任何剩余的 PDF。这是我的代码:
import os
from PyPDF2 import PdfFileReader, PdfFileMerger
originalFile="C:/originalFile.pdf"
merger = PdfFileMerger()
merger.append(PdfFileReader(file(originalFile, 'rb')))
os.remove(originalFile)
for filename in os.listdir('C:/'):
if "Mango" in filename or "Apple" in filename or "Orange" in filename:
if ".pdf" in filename:
merger.append(PdfFileReader(file('C:/'+filename, 'rb')))
os.remove("C:/"+filename)
for filename in os.listdir('C:/'):
if "Zebra" in filename or "Monkey" in filename or "Dog" in filename:
if ".pdf" in filename:
merger.append(PdfFileReader(file('C:/'+filename, 'rb')))
os.remove("C:/"+filename)
for filename in os.listdir('C:/'):
if ".pdf" in filename:
merger.append(PdfFileReader(file('C:/TRIAL/'+filename, 'rb')))
os.remove("C:/TRIAL/"+filename)
merger.write(originalFile)
当我运行这个程序时,我得到以下错误:
os.remove(originalFile) WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'C:/originalFile.pdf'
谁能解释我将文件添加到合并文件后如何关闭文件?