C2W1 Assignment - Failed test case

I passed the assignment, but I got the following message for my split_data function:

Failed test case: incorrect number of (training, validation) images when using a split of 0.5 and 12 images (6 are zero-sized).
Expected:
(3, 3),
but got:
(4, 5).

I don’t understand why my function would not work for this case. Below are my code for the function:

def split_data(SOURCE_DIR, TRAINING_DIR, VALIDATION_DIR, SPLIT_SIZE):
      file_list = os.listdir(SOURCE_DIR)
      for file in file_list:
           path = os.path.join(SOURCE_DIR, file)
           if os.path.getsize(path) == 0:
              print(f'{file} is zero length, so ignoring.')
              file_list.remove(file)
   
      file_list = random.sample(file_list, len(file_list))
      target = int(len(file_list)*SPLIT_SIZE)

      for train_file in file_list[0:target]:
           copyfile(os.path.join(SOURCE_DIR, train_file), os.path.join(TRAINING_DIR, train_file))
  
      for val_file in file_list[target :len(file_list)]:
         copyfile(os.path.join(SOURCE_DIR, val_file), os.path.join(VALIDATION_DIR, val_file))

Thanks!

list.remove will remove only the 1st match of the parameter. So, if adjacent files are invalid, you’ll skip over one of them.

Here’s an example using strings:

In [1]: lst = ["remove_me_1", "remove_me_2"] + ["i'm ok1", "i'm ok 2"]

In [2]: lst
Out[2]: ['remove_me_1', 'remove_me_2', "i'm ok1", "i'm ok 2"]

In [3]: for item in lst:
   ...:     if "remove_me" in item:
   ...:         lst.remove(item)
   ...: 

In [4]: lst
Out[4]: ['remove_me_2', "i'm ok1", "i'm ok 2"]
2 Likes

Thanks for the explanation! I understand it now.

I’ve fixed it using list comprehension. Thanks!