I have a lot of pictures in a paste following a pattern for the file name, they only differ in the file type which may be .jpg or .jpeg For instance:
IMG-20211127-WA0027.jpg
IMG-20211127-WA0028.jpeg
IMG-20211127-WA0029.jpg
I'm trying to find a way to create a folder for each year and send the pictures for the respective folder, given that the file name already has its year. How can I create folders for each year, move the files to the right folder?
I tried to adapt a code from a tutorial, but I'm not getting what I need. Please see my code below :
from distutils import extension
import os
import shutil
path = "D:\WhatsApp Images"
files = os.listdir(path)
year = os.path.getmtime(path)
for file in files:
filename, extension = os.path.splitext(file)
extension = extension[1:]
if os.path.exists(path+'/'+extension):
shutil.move(path+'/'+file, path+'/'+extension+'/'+file)
else:
os.makedirs(path+'/'+extension)
shutil.move(path+'/'+file,path+'/'+extension+'/'+file)