user10976183
user10976183

Reputation: 287

I keep getting a WinError 2 and WinError 3 when trying to move specific files to another folder

So I have a really big folder from a dataset downloaded online that has almost 5500 folder each containing 6 or so images. Some of those images have cloth_front_mask in them.

Since there are way to many folder for me to go through each one of them and try to move the files to another directory, I tried creating a small script that would do that task for me but when I tried it I get a

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'

Here's my code:

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)

Upvotes: 0

Views: 557

Answers (1)

user10976183
user10976183

Reputation: 287

I got it. The problem was that in the shutil.move line I didn't specify the file's original folder. When I did so, it worked fine.

Upvotes: 1

Related Questions