Reputation: 45
I have a folder(Fruits
) that contains both apple and orange images. Let's say fifty per each. And the folder contains total 100 images.
In this fruit folder, Apple images are held like apple0.jpg , apple1.jpg ........ apple49.jpg
On the other hand, orange images are held like orange0.jpg, orange1.jpg, orange2.jpg........ orange49.jpg
So with that datas, how can I create a Apple folder and copy all apple images there. Not only this, but also, how can I create Orange file and copy all the orange images to that Orange file.
I'm using Python programming language. Is there any tutorial about this, or do you have any idea how can I perform this.
Thank you for all of your comments. I am planning to do these in Google colab.
Upvotes: 0
Views: 65
Reputation: 777
Assuming you mean you want to create a new folder Apples and Oranges to move the files in, if the files can be relied to start with apple or oranges, you can use the following simple bruteish code :-
import os
import shutil
files = os.listdir('Fruits')
if not os.path.isdir('Oranges'):
os.mkdirs('Oranges')
if not os.path.isdir('Apples'):
os.mkdirs('Apples')
for f in files:
if not f.endswith('.jpg'):
continue
if f.startswith('apple'):
target = os.path.join('Apples', f)
elif f.startswith('orange'):
target = os.path.join('Oranges', f)
shutil.move(os.path.join('Fruits', f), target)
Upvotes: 2