pythonloopsfile-conversionimg2pdf

Is there a way to order imported files before they are brought into a loop?


I am attempting to convert all .job files in a folder into a single pdf. this code does that however they are in no particular order. I would like them to be imported in the order of the time the file was created or their filename it follows a set pattern 'XXX_1.jpg'

This is what I have so far:

import img2pdf
os.chdir('C:/Path')
# convert all files ending in .jpg inside a directory

with open("output.pdf", "wb") as f:
    f.write(img2pdf.convert([i for i in os.listdir('.') if i.endswith(".jpg")]))

Solution

  • First, you can use glob to gather all the paths of the files in your directory into a list. Then with os module getctime, you can get the list of time of creation. I zipped both list, then made a dictionary whose keys are the file path and values - time of creation. Finally i got the dictionary arrange by values using the operator module to arrange all dictionary in descending order of values(i.e newest file first)

    import os
    import glob
    import operator
    import img2pdf
    
    a= glob.glob("my_directory/*.jpg")
    b = [os.path.getctime(i) for i in a]
    c = {}
    for i,j  in list(zip(a,b)):
        c[i] = j
    sorted_c = dict(sorted(c.items(), key=operator.itemgetter(1),reverse=True))
    with open("output.pdf", "wb") as f:
        f.write(img2pdf.convert([k for k in sorted_c]))