I have several image/files on my computer. All of which start like this 1214455.jpeg
Each image is either a cat or a dog. The digits is a serial number that can be found in the database.
I want to separate images for cat and dog, and place them into two separate folders.
Every image has a name, which is the same serial number.
I want to be able to use a tuple of serial number / names as string to find the names of cats and dog. Then separate them.
source_dir = “C: file1”
destination_dir = (
“C: cats”
)
destination_dir = (
“C: dogs”
)
file_names = os.listdir(source_dir)
files = os.listdir(source_dir)
output=(“123344”, “345566”, “8888887”)
for file_name_i in files:
# for file_name_i in os.listdir(current_dir): # to search in sub folders
# if the file starts with a number as string in the list, place in cancer folder if not place in health
if os.path.join(source_dir, file_name_i).startswith(output):
shutil.move(os.path.join(source_dir, file_name_i), destination_dir, "cats")
else:
shutil.move(os.path.join(source_dir, file_name_i), destination_dir, "dogs")
This code only remove every image. It is unable separate the animals.
Ricardo Mitchell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
shutil.move takes 3 arguments (source, destination, copy_function)
, and you are passing (source, destination, copy_function="cats")
or copy_function="dogs"
respectively.
Leave the third argument blank (unless there is a copy function you want to use), and instead of destination_dir
use f"{destination_dir}/cats"
, and f"{destination_dir}/dogs"
respectively.