Shutil copy2 说它复制了文件,但我实际上无法在 Windows 资源管理器中看到它们

问题描述 投票:0回答:1
def do_it():
    global files_to_move
    global copymove

    for file_dict in files_to_move.values():
        if check_path(file_dict['new_path']) == False:
            return
        
        if (file_dict['disabled']):
            continue

        new_name = file_dict['new_path']+'\\'+file_dict['new_name']
        if os.path.exists(new_name):
            status_update(f"Error! the file we were going to move already exists? {new_name}")
            continue
        try:
            if copymove == 'MOVE':
                os.rename(file_dict['path_file'], new_name)
                status_update(f"Moved {file_dict['path_file']} to { new_name}")
            else:
                shutil.copy2(file_dict['path_file'], new_name)
                status_update(f"Copied {file_dict['path_file']} to { new_name}")

        except OSError as e:
            status_update(f'Error renaming file: {e}')
    update_files()
    return

使用上面的代码,我有一个文件列表(文件信息的字典,包括名称、路径、新名称等),我想将其从旧位置复制到新位置。当我运行它时,它似乎起作用并表示文件已复制。然而,它们实际上并没有显示在我计算机上的 Windows 资源管理器中,所以我认为它失败了。

但是,当我更新文件列表时,它会在我执行 os.listdir(path) 时检测到文件。它以某种方式检测未复制的文件。如果我再次运行该操作(在复制以适应之前检测冲突并重命名文件),我最终会得到重复的虚拟文件。这是怎么回事!?

如何使文件复制真正完成?我应该在代码中使用什么来使这项工作正常进行?

python windows copy
1个回答
0
投票

在上面休息并使用一些手动复制命令回来测试给定的源文件和目标之后,我意识到我的路径中有一个错误。事实证明,GD Windows 将照片文件夹即时重命名为“图片”,因为它必须是特殊的 - 实际路径是“照片”,我正在创建并复制到它旁边的一个名为“图片”的文件夹(因为这就是什么)它显示在 Windows 资源管理器中)。

© www.soinside.com 2019 - 2024. All rights reserved.