import os # Using system functions import shutil # Move the file to different folders. # Change the folder location. os.chdir (os.getcwd ()) cwd = os.getcwd () # To print files from a folder. l = [f for f in os.listdir (cwd) if os.path.isfile (f)] l2 = [] # Get file extension from l list. for value in l: s = value.split ( ’.’ ) [ 1 ] l2.append (s) print (l, l2) # Remove duplicate values from # list l2 and check if the directory is # exists otherwise we create a new one for extension in set (l2): dirname = extension if os.path.exists (cwd + ’’ + extension): pass else : os.makedirs (dirname) # We use the zip function and enumerate l and # l2 passed as an argument. # If the extension in the file is the same # file does not exist, we are moving the file. for files, extension in zip (l, l2): if extension in file s: if os.path .exists (cwd + ’’ + extension + ’ ’ + files): pass else : shutil.move (cwd + ’ ’+ files, cwd +’ ’ + extension) print (extension, files) else : print ( ’ error’ ) |