首页 > 解决方案 > shutil 说子文件夹正在使用中,因此无法移动它并且程序崩溃但一切都已成功移动

问题描述

在我发出移动命令之前,我正在使用 shutil 移动一个文件夹,其中包含我的程序正在使用的一堆东西。此时我关心的所有内容都已从内存中卸载,因此每个文件都按预期成功移动到目标文件夹。神秘地,一个有问题的子文件夹仍然存在(即使在目标位置创建了一个类似的子文件夹)并且程序抛出以下错误:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:
    'C:\\Users\\Ze\\PycharmProjects\\Ploutos\\ProductList\\Unedited\\1005001769427465\\Images'

我要移动的文件夹是编号 ID 1,其中包含有问题的“图像”文件夹。

编辑:据我所知,除了我自己的程序和 Pycharm 正在使用该文件夹之外,没有其他程序。尝试重新索引文件时可能是 Pycharm 吗?

相关代码:

def move_product_to_edited(product_id):
    global products_edited_during_session_counter

    unedited_products_path = application_path + '\\ProductList\\Unedited\\'

    edited_products_path = application_path + '\\ProductList\\Edited\\'

    # if the product has not yet been moved, move it
    if os.path.exists(unedited_products_path + str(product_id)):
        shutil.move(unedited_products_path + str(product_id), edited_products_path + str(product_id))

我该如何尝试进一步调试呢?我不知道如何检查为什么该文件夹仍在使用中。

标签: pythonshutil

解决方案


推荐阅读