当我尝试将特定文件移动到另一个文件夹时,会不断收到WinError 2和WinError 3

2024-06-01 19:40:17 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个很大的文件夹,它来自一个在线下载的数据集,几乎有5500个文件夹,每个文件夹包含6幅左右的图像。有些图像中有布面遮罩

因为有很多方法可以让我浏览其中的每一个文件夹,并尝试将文件移动到另一个目录,所以我尝试创建一个小脚本来完成这项任务,但当我尝试时,我得到了一个

Traceback (most recent call last):
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 557, in move
    os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: '10K21D003-C11@2=cloth_front_mask.jpg' -> 'C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\cloth_mask\\10K21D003\\10K21D003-C11@2=cloth_front_mask.jpg'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#52>", line 9, in <module>
    shutil.move(name, path, copy_function=shutil.copytree)
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 571, in move
    copy_function(src, real_dst)
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 309, in copytree
    names = os.listdir(src)
FileNotFoundError: [WinError 3] The system cannot find the path specified: '10K21D003-C11@2=cloth_front_mask.jpg'

这是我的密码:

for root, dirs, files in os.walk('.'):
    for name in files:
        if 'mask' in name:
            path = os.path.join('C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\cloth_mask', name[:9])
            if os.path.exists(path):
                pass
            else:
                os.mkdir(path)
            shutil.move(name, path, copy_function=shutil.copytree)

Tags: pathnamein文件夹moveoslinemask