多文件夹图片转PDF脚本CMD执行即退出,Pycharm显示退出码0求助
Python图片转PDF脚本无输出(退出码0)
我编写了一个用于将多文件夹中的图片转换为PDF进行归档的Python脚本,昨日该脚本正常完成了245个文件夹的转换任务,但今日在CMD中执行时会立即返回命令提示符,在PyCharm中调试则显示退出码0,无任何输出。
脚本代码
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中会打印处理日志,输出文件夹生成对应PDF文件

- 实际结果:CMD无任何输出,直接返回命令提示符

注:为保护隐私,已对文件名打码。
问题分析与解决方案
核心原因
退出码0说明脚本无异常正常执行完毕,大概率是所有待处理文件夹已被标记为已处理,导致循环未进入任何处理逻辑。
排查步骤
- 检查
E:/processed_folders.log文件内容,确认是否包含E:/RESIDENTIAL下的所有子文件夹。如果是,说明没有未处理任务,脚本自然无输出。 - 若存在未处理文件夹,检查日志中记录的路径与脚本生成的
folder_path是否完全一致(比如Windows下反斜杠\与正斜杠/的差异、大小写差异)——集合匹配要求字符串完全一致,路径格式不匹配会导致误判,但脚本无输出说明更可能是所有文件夹已处理。
优化与修复方案
- 添加调试输出:在循环前加入统计代码,明确待处理文件夹数量:
print(f"已处理文件夹数量:{len(processed_folders)}") all_folders = [os.path.join(root_folder, name) for name in os.listdir(root_folder) if os.path.isdir(os.path.join(root_folder, name))] print(f"总文件夹数量:{len(all_folders)}") unprocessed = [f for f in all_folders if f not in processed_folders] print(f"待处理文件夹数量:{len(unprocessed)}") - 完善日志文件处理:修改
load_processed_folders函数,处理日志文件不存在的情况:def load_processed_folders(log_file): if not os.path.exists(log_file): return set() with open(log_file, 'r') as f: return set(line.strip() for line in f if os.path.isdir(line.strip())) - 修正已处理记录逻辑:当前脚本无论处理成功还是失败(如内存错误)都会标记为已处理,导致失败的文件夹无法重试。修改为仅成功生成PDF后才记录:
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) save_processed_folder(log_file, folder_path) # 成功才记录已处理 except MemoryError: print("Memory error occurred while processing folder:", folder_path) log_memory_error(folder_path, memory_error_log_file) # 不记录已处理,下次可重试
内容的提问来源于stack exchange,提问作者Donzskie Just Passing By
相关产品推荐
相关产品推荐

