From d783b269ea941de8b89d98bc74fc6918c15e89ad Mon Sep 17 00:00:00 2001 From: Vardhan Hegde Date: Thu, 17 Oct 2024 17:15:35 +0530 Subject: [PATCH 1/2] file_organizer.py file updation with more features. --- File-Organizer/README.md | 10 ++- File-Organizer/file-organizer.py | 138 ++++++++++++++++++++++++++----- File-Organizer/requirements.txt | 4 +- 3 files changed, 128 insertions(+), 24 deletions(-) diff --git a/File-Organizer/README.md b/File-Organizer/README.md index c4468ae3..56fd4074 100644 --- a/File-Organizer/README.md +++ b/File-Organizer/README.md @@ -1,8 +1,14 @@ # File Organizer Organizes your files in the folder according to their extension by grouping them together. +Works on both Windows and Linux based operating systems. # Libraries Used -- +os +path +shutil # Usage -Just run the command `python3 file-organizerpy` from your terminal/bash. +Just run the command `python3 file-organizer.py` from your terminal/bash. +Will ask for to organize current 1.directory(folder) or specific 2.directory(folder), +If '2' is selected please copy paste or type the folder absolute path. + \ No newline at end of file diff --git a/File-Organizer/file-organizer.py b/File-Organizer/file-organizer.py index 3ce3af97..7bfc9fad 100644 --- a/File-Organizer/file-organizer.py +++ b/File-Organizer/file-organizer.py @@ -1,24 +1,120 @@ import os +import pathlib import shutil - -#The Path of the directory to be sorted -path = 'C:\\Users\\\\Downloads' -#This populates a list with the filenames in the directory -list_ = os.listdir(path) - -#Traverses every file -for file_ in list_: - name,ext = os.path.splitext(file_) - print(name) - #Stores the extension type - ext = ext[1:] - #If it is directory, it forces the next iteration - if ext == '': - continue - #If a directory with the name 'ext' exists, it moves the file to that directory - if os.path.exists(path+'/'+ext): - shutil.move(path+'/'+file_,path+'/'+ext+'/'+file_) - #If the directory does not exist, it creates a new directory + +''' +Different type of file formats +''' + +fileFormat = { +"Web": [".html5", ".html", ".htm", ".xhtml",".csv",".webp",".acsm"], + +"Picture": [".jpeg", ".jpg", ".tiff", ".gif",".heic",".HEIC" + ".bmp", ".png", ".bpg", ".svg", ".heif", ".psd"], + +"Video": [".avi", ".mkv", ".flv", ".wmv", + ".mov", ".mp4", "..webm", ". vob", + ".mng", ".qt", ".mpg", ".mpeg", ".3gp"], + +"Document": [".oxps", ".epub", ".pages", ".docx", + ".txt", ".pdf", ".doc", ".fdf", + ".ods", ".odt", ".pwi", ". xsn", + ". xps" , ".dotx", ".docm", ".dox", + ".rvg", ".rtf", ".rtfd", ".wpd", + ".xls", ".xlsx", ".ppt", "pptx" , ".md"], +"PPT" : [".pptx"], + +"Compressed": [".a", ".ar", ".cpio", ".iso", + ".tar", ".gz", ".rz", ".7z", + ".dmg", ".rar", ".xar", ".zip", + ".msi",".msix"], + +"Audio": [".aac", ".aa", ".aac", ".dvf", + ".m4a", ".m4b", ".m4p", ".mp3", + ".msv", "ogg", "oga", ".raw", + ".vox", ".wav", ".wma", "aiff"], + +"Torrent":[".srt",".torrent"], + +"Installable":[".exe"], + +"apk":[".apk"], + +"c_cpp":[".c",".cpp"], + +"python":[".py",".ipynb"], + +"JSON":[".json"], + +"font_style":[".otf"], + +"CSV_XL" : [".csv",".xlsx",".xlsm","xlsb"] + +} + +fileTypes = list(fileFormat.keys()) +fileFormats= list(fileFormat.values()) + +def organizer(directory): + + files_of_the_folder = os.listdir(directory) + directory += '/' + + for file in files_of_the_folder: + fileName = pathlib.Path(file) + fileFormatType = fileName.suffix.lower() + + if (fileName != "file_organizer" and fileFormatType != ".py"): #to exclude file_organizer.py file + src = directory + str(fileName) + destination = directory + "Other" #default destination for the file is 'Other' folder + + if fileFormatType == "": + print(f"{src} has no file Format") + else: + for formats in fileFormats: + + if fileFormatType in formats: #checking for file format + folder = fileTypes[fileFormats.index(formats)] # getting the folder name for the file format + + if os.path.isdir(directory + folder) == False: #if the folder doesn't exist creat new folder(specific to the file format) + os.mkdir(directory + folder) + destination = directory + folder + + else: + if os.path.isdir("Other") == False: + os.mkdir(destination + "Other") #if 'Other' folder doesn't exist create it. + print(src, " ----------moved to--------> ", destination, ",") #bash : message + try: + shutil.move(src, destination) + except: + continue + else: + pass + + print("Files are organized.... files with uncommon extensions might be store in 'Others' folder. Please Check....") #bash : completion message + + +while(True): + choices = input("Would you like to organize: \n1. Current file directory?(type 1/Y) \n2. Different directory?(type 2/N) \n3. Type '3/exit' to exit \nYOUR CHOICE : ") + print() + + if choices.upper() == 'Y' or choices == '1' : #organize current folder + organizer(os.getcwd()) + break + + elif choices.upper() == 'N' or choices == '2': #organize specific folder + path = input("Specify the path of the Directory(Folder) you want to organize ; [provide absolute path] ") + if os.path.exists(path): + print(" The path exists.\n") + organizer(path) + break + + else: + print("The path does not exist.\n") + continue + + elif choices.upper() == 'EXIT' or choices == '3': + print("Exiting...") + break else: - os.makedirs(path+'/'+ext) - shutil.move(path+'/'+file_,path+'/'+ext+'/'+file_) + continue diff --git a/File-Organizer/requirements.txt b/File-Organizer/requirements.txt index 3d90aaa5..9d199562 100644 --- a/File-Organizer/requirements.txt +++ b/File-Organizer/requirements.txt @@ -1 +1,3 @@ -colorama \ No newline at end of file +os +path +shutil \ No newline at end of file From 4a546c73b27b192230a2bcded66135030932252b Mon Sep 17 00:00:00 2001 From: Vardhan Hegde Date: Thu, 17 Oct 2024 17:30:01 +0530 Subject: [PATCH 2/2] Update file-organizer.py --- File-Organizer/file-organizer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/File-Organizer/file-organizer.py b/File-Organizer/file-organizer.py index 7bfc9fad..ffc3fd65 100644 --- a/File-Organizer/file-organizer.py +++ b/File-Organizer/file-organizer.py @@ -81,8 +81,8 @@ def organizer(directory): destination = directory + folder else: - if os.path.isdir("Other") == False: - os.mkdir(destination + "Other") #if 'Other' folder doesn't exist create it. + if os.path.isdir(directory +"Other") == False: + os.mkdir(directory + "Other") #if 'Other' folder doesn't exist create it. print(src, " ----------moved to--------> ", destination, ",") #bash : message try: shutil.move(src, destination)