Organize files according to their extensions using python

I’m a beginner in python, im trying to develop a mini project, for instance program will prompt a user to enter a path or directory name, where he or she needs to sort out extension files in a directory and the program will enter directory and sort the files,here is my code its not working perfect.

import os
import sys
import shutil #Moving file to different folder
import subprocess

#path = “C:\User\apt_2\Downloads” #Directory that needs to be sorted

def organise_files_ext():

     print("Enter directory  or the folder path")
     path = input()
     lst = os.listdir(path)

     for f in lst:   #Iterate through each and every file
        name, ext = os.path.splitext(f)
        ext = ext[1:] #store extension type

        if ext == '': #Continue to next iteration if its a directory
        if os.path.exists(path+'/'+ext):    #Move the file to the directory where the name 'ext' already exists

    #Create new directory, if the directory does not already exist


except IOError:
    print("Invalid Path")
    return _menu()
   # os.system("cls")

i will appreciate any input to improve my code

du you have a particulare issue/error or is it just about possible optimizing of the logic/flow?

yes i mean optimizing the logic flow of my code.
be able to read the correct path or directory from user input, for instance if i want to organise files extension in the C:\Users\Documents.
and able to organize extension files accordingly from the given path or directory, create a new directory for each file extension.

This topic was automatically closed after 121 days. New replies are no longer allowed.