在 cmd 中运行此脚本只会转到下一行,如果我在 Pycharm 中调试它,它总是显示退出代码 0

问题描述 投票:0回答:1

我制作了这个脚本,用于将多个文件夹中的图像转换为 PDF 以便存档,昨天它运行良好,转换了 245 个文件夹,现在如果我尝试在 cmd 中执行该脚本,它总是会转到下一行

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)

这应该是你在cmd和指定文件夹中看到的内容

我今天得到的输出 P.S 为了隐私,我在文件名上加了一个框

谢谢!

python cmd
1个回答
0
投票

这是因为您没有处理已经处理过的文件夹。因此,脚本无需执行任何操作。如果您想看到相同的输出,请进行以下修改:

更换

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

    if os.path.isdir(folder_path):
© www.soinside.com 2019 - 2024. All rights reserved.