0

I want to move an image of dogs and cats from the train directory into two directories train/dogs and train/cats to be ready to use PyTorch's ImageFolder function. However when done without / at the end of the new directories it seems that instead of moving the files to the new train/dogs folder for example it moves the image to a train/dogs file

# we move dogs to train/dogs and cats to train/cats
import re

train_dir = "train"
train_dogs_dir = f'{train_dir}/dogs'
train_cats_dir = f'{train_dir}/cats'

files = os.listdir(train_dir)

for f in files: 
    catSearchObj = re.search("cat", f)
    dogSearchObj = re.search("dog", f)
    if catSearchObj:
        shutil.move(f'{train_dir}/{f}', train_cats_dir)
    elif dogSearchObj:
        shutil.move(f'{train_dir}/{f}', train_dogs_dir)

Indeed, after that when he did ! the train/dogs answers me:

train/dogs

And with ! cat train/dogs:

����JFIF��C



#%$""!&+7/&)4)!"0A149;>>>%.DIC<H7=>;��C
;("(;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;��w�"��   
���}!1AQa"q2��#B��R��$3br�  
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������    
Revolucion for Monica
  • 2,848
  • 8
  • 39
  • 78

1 Answers1

0

The shutil docs for move() say "If the destination is an existing directory, then src is moved inside that directory." (emphasis added)

Is it possible your target directories don't exist?

You may need to create those directories if they don't exist.

jgreve
  • 1,225
  • 12
  • 17