I'm learning python and I would like to iterate a lot of files that are no consecutives in the enumeration. For example, the name of my files that I need to merge:
dp_1.pdf I need it to merge with support_dp_1.pdf
dp_3.pdf - - - - - - - - - - - - support_dp_3.pdf
ip_1.pdf - - - - - - - - - - - - support_ip_1.pdf
ip_2.pdf - - - - - - - - - - - - support_ip_2.pdf
ip_3.pdf - - - - - - - - - - - - support_ip_3.pdf
ep_4.pdf - - - - - - - - - - - - support_ep_4.pdf
My script is this:
from PyPDF2 import PdfFileMerger
files = ['dp_', 'ep_', 'ip_']
number = 1
while number <= 4:
for f in files:
pdfs = [f + str(number) + ".pdf", "support_" + f + str(number) + ".pdf"]
name_file_output = "output_" + f + str(number) + ".pdf"
fusion = PdfFileMerger()
for pdf in pdfs:
fusion.append(open(pdf, 'rb'))
with open(name_file_output, 'wb') as output:
fusion.write(output)
number += 1
And I have this error:
FileNotFoundError: [Errno 2] No such file or directory: 'dp_2.pdf'
And the program exit. How can I iterate without exit and continue with the next files in the loop?