Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file_organizer.py file updation with more features. #390

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions File-Organizer/README.md
Original file line number Diff line number Diff line change
@@ -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.

138 changes: 117 additions & 21 deletions File-Organizer/file-organizer.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,120 @@
import os
import pathlib
import shutil

#The Path of the directory to be sorted
path = 'C:\\Users\\<USERNAME>\\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(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)
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
4 changes: 3 additions & 1 deletion File-Organizer/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
colorama
os
path
shutil