Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

running this script in cmd will only go to the next line, and if I debugged it in Pycharm it always say exit code 0

I made this script for me to convert images to PDF in multiple folders for archiving, yesterday it runs fine doing 245 folders to convert and now today if I try to execute the script in cmd it always go to the next line

import os
from PIL import Image

def convert_images_to_pdf(folder_path, output_pdf):
    image_files = [os.path.join(root, file) for root, _, files in os.walk(folder_path)
                   for file in files if file.lower().endswith(('jpg', 'jpeg', 'png'))]
    
    if not image_files:
        print("No image files found in", folder_path)
        return

    try:
        first_image = Image.open(image_files[0])
        first_image.save(output_pdf, "PDF", resolution=100.0, save_all=True,
                         append_images=[Image.open(image_file) for image_file in image_files[1:]])
        print("PDF saved to", output_pdf)
    except MemoryError:
        print("Memory error occurred while processing folder:", folder_path)
        log_memory_error(folder_path, "E:/memory_error.log")

def load_processed_folders(log_file):
    return set(line.strip() for line in open(log_file, 'r') if os.path.isdir(line.strip()))

def save_processed_folder(log_file, folder_path):
    with open(log_file, 'a') as f:
        f.write(folder_path + '\n')

def log_memory_error(folder_path, log_file):
    with open(log_file, 'a') as f:
        f.write(folder_path + '\n')

# Example usage
root_folder = "E:/RESIDENTIAL"
output_folder = "E:/RESIDENTIAL_OUTPUT"
log_file = "E:/processed_folders.log"
memory_error_log_file = "E:/memory_error.log"

processed_folders = load_processed_folders(log_file)

for folder_name in os.listdir(root_folder):
    folder_path = os.path.join(root_folder, folder_name)
    if os.path.isdir(folder_path) and folder_path not in processed_folders:
        output_pdf = os.path.join(output_folder, folder_name + ".pdf")
        convert_images_to_pdf(folder_path, output_pdf)
        save_processed_folder(log_file, folder_path)

This should be what you see in the cmd and specified folder

The output i got today
P.S I put a box on the file names for privacy

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is because you are not processing the folders that are already processed. Hence, there is nothing to do by the script. If you want to see same output, do following modification:

Replace

    if os.path.isdir(folder_path) and folder_path not in processed_folders:

With

    if os.path.isdir(folder_path):
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading