سؤال

I'm trying to write a program in python that takes a PDF file and appends to it first any pdf which includes the name of a fruit to it(Mango, Orange or Apple), then appends the pdf's with the names of animals to the original file(Zebra, Monkey, Dog) and finally appends any remaining PDF's. This is the code I have:

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)

When I run this program I get the following Error:

os.remove(originalFile) WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'C:/originalFile.pdf'

Could anyone explain me how to close the file after I've added it to my merger file?

هل كانت مفيدة؟

المحلول

You should close the file explicitly.

fd = file('C:/'+filename, 'rb')
merger.append(PdfFileReader(fd))
fd.close()
os.remove('C:/'+filename)

A safer version:

fd = None
try:
    fd = file('C:/'+filename, 'rb')
    merger.append(PdfFileReader(fd))
finally:
    if fd: fd.close()
if os.path.exists('C:/'+filename): os.remove('C:/'+filename)

Which can be simplified in Python 2.5+ as:

with file('C:/'+filename, 'rb') as fd:
    merger.append(PdfFileReader(fd))
if os.path.exists('C:/'+filename): os.remove('C:/'+filename)

Which will cause python to close the file automagically.

نصائح أخرى

To close a file, you should have opened it with with statement, which always closes the file whatever happens to the code inside the with block:

with open(originalFile,'rb') as pdf:
    merger.append(PdfFileReader(pdf))
os.remove(originalFile)

This works for me.

Just a reminder that, you can close the file since you have added the pdf into the merger. Note that if you just open it with PdfFileReader(pdf) and haven't done anything to it, you can't delete the file or the PdfFileReader object won't be able to read the file. This is because the PdfFileReader only actually reads the file if you call some read method on it like getPage

Become originalFile has been opened, therefore, you cannot delete the file until you close it. You need to modify your code like this:

merger = PdfFileMerger()
fin = file(originalFile, 'rb')
merger.append(PdfFileReader(fin))
fin.close()
os.remove(originalFile)

PyPDF merger now has a close method in as of in version 1.26.0

close()

Shuts all file descriptors (input and output) and clears all memory usage.

https://pythonhosted.org/PyPDF2/PdfFileMerger.html

Pdf merging isn't that hard in python. I see that you are already using PdfFileMerger. That should work as long as the pdf file exists, and the user who forks the python process has privileges to access the pdfs being merged. Good luck.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top