Home > Software engineering >  Condition to check before merging pdf files
Condition to check before merging pdf files

Time:04-24

Hi I have written a program which merge pdf files from three different folders

These 3 different folders have pdf files containing different data but with same file name.

We have to merge files with same name.

I have written the following logic

for file1 in f1_files :
    for file2 in f2_files:
        for file3 in f3_files:
            if file1 == file2:
                if file2 == file3:
                 pdf_merger(file1,file2,file3)
                else: 
                     print(file2, "in marketing folder doesnt have a corresponding file in Startegy folder")
            else:
                print(file1, "in form folder doesnt have a corresponding file in marketing folder" )

it merges the files but i dont think so the logic is working as intended: I get the below output

4865.pdf in Marketing folder doesnt have a corresponding file in Startegy folder
4865.pdf in Marketing folder doesnt have a corresponding file in Startegy folder
4865.pdf in Form folder doesnt have a corresponding file in Marketing folder
4865.pdf in Form folder doesnt have a corresponding file in Marketing folder
4865.pdf in Form folder doesnt have a corresponding file in Marketing folder
4865.pdf in Form folder doesnt have a corresponding file in Marketing folder
4865.pdf in Form folder doesnt have a corresponding file in Marketing folder
4865.pdf in Form folder doesnt have a corresponding file in Marketing folder
4866.pdf in Form folder doesnt have a corresponding file in Marketing folder
4866.pdf in Form folder doesnt have a corresponding file in Marketing folder
4866.pdf in Form folder doesnt have a corresponding file in Marketing folder
4866.pdf in Marketing folder doesnt have a corresponding file in Startegy folder
4866.pdf in Marketing folder doesnt have a corresponding file in Startegy folder
4866.pdf in Form folder doesnt have a corresponding file in Marketing folder
4866.pdf in Form folder doesnt have a corresponding file in Marketing folder
4866.pdf in Form folder doesnt have a corresponding file in Marketing folder
4867.pdf in Form folder doesnt have a corresponding file in Marketing folder
4867.pdf in Form folder doesnt have a corresponding file in Marketing folder
4867.pdf in Form folder doesnt have a corresponding file in Marketing folder
4867.pdf in Form folder doesnt have a corresponding file in Marketing folder
4867.pdf in Form folder doesnt have a corresponding file in Marketing folder
4867.pdf in Form folder doesnt have a corresponding file in Marketing folder
4867.pdf in Marketing folder doesnt have a corresponding file in Startegy folder
4867.pdf in Marketing folder doesnt have a corresponding file in Startegy folder

Can someone help on this

CodePudding user response:

This should work in your case

for file1 in f1_files :
    for file2 in f2_files:
        for file3 in f3_files:
            if file1 == file2 and file2 == file3:
                 pdf_merger(file1,file2,file3)
                else: 
                 print('No matching files')

CodePudding user response:

Used i.e. os module to list the content of a directory. All the loops iterate over the list of the basenames of their corresponding dir which means that before passing the files to the pdf_merger function you need to rebuild the original paths with os.path.join.

Remark: simultaneous comparison support transitivity;)

import os

# set your paths as dict-values
dirs = {'path1': 'path2dir1', 'paths2': 'path2dir2', 'paths3': 'path2dir3'}

# get dirs content
for file1 in os.listdir(dirs['path1']):
    for file2 in os.listdir(dirs['path2']):
        for file3 in os.listdir(dirs['path3']):
            # simultaneous check
            if file1 == file2 == file3:
                # rebuild the paths
                file1 = os.path.join(dirs['path1'], file1)
                file2 = os.path.join(dirs['path2'], file2)
                file3 = os.path.join(dirs['path3'], file3)
                # call the function
                pdf_merger(file1, file2, file3)
            else:
                # if-else case studies
                pass
  • Related