Home > Software engineering >  Python move pictures from one folder to another ignoring case
Python move pictures from one folder to another ignoring case

Time:10-04

I am trying to create a script to move all the picture files from one folder to another. I have found the script that works for this, except it doesn't work if the extensions are in capitals. Is there an easy way around this?

Current code:

import shutil

import os

source = "C:/Users/Tonello/Desktop/"

dest = "C:/Users/Tonello/Desktop/Pictures/"

files = os.listdir(source)

for f in files:

    if os.path.splitext(f)[1] in (".jpg", ".gif", ".png"):

         shutil.move(source   f, dest)

CodePudding user response:

You could lowercase the extension before checking it:

if os.path.splitext(f)[1].lower() in (".jpg", ".gif", ".png"):
    # Here --------------^
  • Related