python的tempfile无法删除Windows上的文件

问题描述

我想使用我在网上找到的此功能

def convert_pdf(file_path,output_path):
# save temp image files in temp dir,delete them after we are finished
with tempfile.TemporaryDirectory() as temp_dir:
    # convert pdf to multiple image
    images = convert_from_path(file_path,output_folder=temp_dir)
    # save images to temporary directory
    temp_images = []
    for i in range(len(images)):
        image_path = f'{temp_dir}/{i}.png'
        images[i].save(image_path,'PNG')
        temp_images.append(image_path)
    # read images into pillow.Image
    imgs = list(map(Image.open,temp_images))
# find minimum width of images
min_img_width = min(i.width for i in imgs)
# find total height of all images
total_height = 0
for i,img in enumerate(imgs):
    total_height += imgs[i].height
    print(img)
# create new image object with width and total height
merged_image = Image.new(imgs[0].mode,(min_img_width,total_height))
# paste images together one by one
y = 0
for img in imgs:
    merged_image.paste(img,(0,y))
    y += img.height
# save merged image
merged_image.save(output_path)
return output_path

但是运行时发生错误

PermissionError:[WinError 32]

我很确定这是因为我必须关闭

imgs = list(map(Image.open,temp_images))

在某个地方,但我不知道如何在何处。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)