I have a code that generates a python list and I sorted this list in such lines
mylist = list_files(MAIN_DIR)
print(sorted(mylist, key=lambda x: str(x.split('\\')[-1][:-4].replace('_',''))))
Now I got the python list sorted as desired. How can I split this main python list into suclists based on the similar pdf names?
Have a look at the snapshot to get what I mean exactly
Now I can loop through the keys and values of the grouped dictionary
for el in mylist:
file_name = el.split('\\')[-1].split('.')[0]
if file_name not in grouped_files.keys():
grouped_files[file_name] = []
grouped_files[file_name].append(el)
for key, value in grouped_files.items():
pdfs = value
merger = PdfFileMerger()
for pdf in pdfs:
merger.append(pdf)
merger.write(OUTPUT_DIR / f'{key}.pdf')
merger.close()
But I got an error AttributeError: 'WindowsPath' object has no attribute 'write'