25
loading...
This website collects cookies to deliver better user experience
os
and shutil
modules.The shutil
module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal.
os
module to manage the path related issues, we can automate the whole process of moving files to folders we want.def Organizer(path, userSelection):
if os.path.exists(path):
for file in os.listdir(path):
filePath = os.path.join(path, file)
if os.path.isdir(filePath):
pass
filename, fileExtension = os.path.splitext(filePath)
if fileExtension in userSelection:
relocatingPath = os.path.join(
path, userSelection[fileExtension])
if not os.path.exists(relocatingPath):
os.makedirs(relocatingPath)
try:
shutil.move(filePath, relocatingPath)
except OSError as error:
print('''Something unexpected happened!
Error message:''', error)
print("\n\tYour files are sorted Successfully!")
path
, userSelection
. We will be defining both in a moment.extenstionSet
for the most common file types. The dictionary will have file extensions(i.e. '.py', '.exe', '.mp4' ...) as keys and folder names(i.e. Movies, Documents ...) as values.
extenstionSet = {
".docx": "Documents",
".xslx": "Documents",
".pdf": "Documents",
".csv": "Documents",
".xlsx": "Documents",
".zip": "Compressed",
".rar": "Compressed",
".mp3": "Musics",
".m4a": "Musics",
".ogg": "Musics",
".wav": "Musics",
".jpg": "Pictures",
".png": "Pictures",
".gif": "Pictures",
".tif": "Pictures",
".mp4": "Videos",
".mkv": "Videos",
".3gp": "Videos",
".mpeg4": "Videos",
}
path = input('''\tPaste the path you want to organize
>>> ''')
while not os.path.exists(path):
print('''\n\tThe path you entered doesn\'t exist.
Make sure there aren\'t spelling errors
and enter the correct path. ''')
path = input('''\n\tPaste the path you want to organize
>>> ''')
extensionSet
dictionaryextensionSet
have limited number of extensions, this option will allow them to sort files with any extension.userSelection = {}
userMenu = 0
while userMenu == 0:
userMenu = input('''
In what way do you want to organize.
1). Organize all files in the directory.
2). Organize files of certain extensions in the directory.
>>> ''')
if userMenu == '1':
userSelection = extenstionSet
elif userMenu == '2':
ext = input('''\n\tEnter the extensions you want to organize
preceeded by a period and separated by space.
For-example:- '.py .json .csv'
>>> ''')
folderName = input('''\n\tEnter a folder name of your choice
for the selected extensions
>>> ''')
for i in ext.split():
userSelection[i] = folderName
else:
print(f'''\n\tYou have entered an invalid input of {userMenu}
Please enter a valid input from the options provided. \n''')
userMenu = 0
if __name__ == "__main__":
Organizer(path, userSelection)