I'm trying to copy 90% of images of a source file into a training folder but I have the following error:
IsADirectoryError Traceback (most recent call last)
<ipython-input-24-e1fe9ed34fea> in <module>
50
51 split_size = .9
---> 52 split_data(CAT_SOURCE_DIR, TRAINING_CATS_DIR, TESTING_CATS_DIR, split_size)
53 split_data(DOG_SOURCE_DIR, TRAINING_DOGS_DIR, TESTING_DOGS_DIR, split_size)
<ipython-input-24-e1fe9ed34fea> in split_data(SOURCE, TRAINING, TESTING, SPLIT_SIZE)
33 for i in training_set:
34 if os.path.getsize(SOURCE + i) != 0:
---> 35 copyfile(SOURCE+i, TRAINING)
36
37 for j in testing_set:
/usr/lib/python3.6/shutil.py in copyfile(src, dst, follow_symlinks)
119 else:
120 with open(src, 'rb') as fsrc:
--> 121 with open(dst, 'wb') as fdst:
122 copyfileobj(fsrc, fdst)
123 return dst
IsADirectoryError: [Errno 21] Is a directory: '/tmp/cats-v-dogs/training/cats'
This is my code:
def split_data(SOURCE, TRAINING, TESTING, SPLIT_SIZE):
list = os.listdir(SOURCE)
images_list = random.sample(list, len(list))
training_set = []
testing_set = []
split_len = int(SPLIT_SIZE*len(list))
for image in range(len(list)):
if image >= split_len:
testing_set.append(list[image])
else:
training_set.append(list[image])
for i in training_set:
if os.path.getsize(SOURCE + i) != 0:
copyfile(SOURCE+i, TRAINING)
for j in testing_set:
if os.path.getsize(SOURCE + j) != 0:
copyfile(SOURCE+j, TESTING)
CAT_SOURCE_DIR = "/tmp/PetImages/Cat/"
TRAINING_CATS_DIR = "/tmp/cats-v-dogs/training/cats"
TESTING_CATS_DIR = "/tmp/cats-v-dogs/testing/cats"
DOG_SOURCE_DIR = "/tmp/PetImages/Dog/"
TRAINING_DOGS_DIR = "/tmp/cats-v-dogs/training/dogs"
TESTING_DOGS_DIR = "/tmp/cats-v-dogs/testing/dogs"
split_size = .9
split_data(CAT_SOURCE_DIR, TRAINING_CATS_DIR, TESTING_CATS_DIR, split_size)
split_data(DOG_SOURCE_DIR, TRAINING_DOGS_DIR, TESTING_DOGS_DIR, split_size)
I think the error is in the function copyfile() but I was sure that I was putting the right paths as arguments, where copyfile(SOURCE+i, TRAINING)
is supposed to copy the image in the source file to the training directory.
I would appreciate any help!
Thank you!
You are using
copyfile
with the wrong destination path.According to the Python docs you need to give the full path with filename.
TRAINING_CATS_DIR
is a directory so it gives theIsADirectoryError
Sidenote: Don't use
list
as a list variable.list
is an inbuilt variable in Python.