Split_data question error

Hi, I am getting strange errors for split_data questsions: FileNotFoundError: [Errno 2] No such file or directory: '918.jpg'

Here is my code:

def split_data(SOURCE, TRAINING, TESTING, SPLIT_SIZE):

  ### START CODE HERE
  images= os.listdir(SOURCE)

  new_images = []
  for i in range(len(images)):
    size = os.path.getsize(images[i])
    if size == 0 or size < 0:
      print('filename is zero length, so ignoring.')
    else:
      new_images.append(images[i])

  trn_images= random.sample(new_images, len(new_images)*SPLIT_SIZE)
  
  copyfile(trn_images, TRAINING)
  copyfile(images[~trn_images], TESTING)

any tip is helpful! thank you

2 Likes

Hi kisax,

The os.path.getsize() works with a path so it needs to be: op.path.getsize(SOURCE/“name of the image”). It is confusing the error said ‘No such file or directory’ because it’s actually asking for the exact path of the file.

Try use the os.path.join() function.

Regards
Weihao

@Weihao_Luo Thank you for the explanation.
It will definitely be useful for other fellow learners as well

Yes @Weihao_Luo the explanation will surely help all fellow learners and also help to resolve the query

1 Like

After using os.path.join function i am getting an error "can’t multiply sequence by non-int of type float

use this code:
int(len(new_images)*SPLIT_SIZE)

because the second argument in sample method must be of type int not float