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():
try:
print("Enter directory or the folder path")
path = input()
os.path.join(path)
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
continue
if os.path.exists(path+'/'+ext): #Move the file to the directory where the name 'ext' already exists
shutil.move(path+'/'+f,path+'/'+ext+'/'+f)
#Create new directory, if the directory does not already exist
else:
os.makedirs(path+'/'+ext)
shutil.move(path+'/'+f,path+'/'+ext+'/'+f)
except IOError:
print("Invalid Path")
return _menu()
# os.system("cls")
i will appreciate any input to improve my code