Python Script to Sort Files based on Filetype Extensions

๐๐๐๐๐๐ ๐ก๐๐๐๐
Search for a command to run...

๐๐๐๐๐๐ ๐ก๐๐๐๐
No comments yet. Be the first to comment.
I'm a Graduate student at UT Arlington. I always feel that taking care of both physically and mentally are essential to our success. So, I start my daily routine at Fitness Recreation Center (gym) and registering slots daily is a hassle and due to co...

You might have seen some people using completely different source labels. Well, today we will see how to change the Twitter source label First, we need to have a Twitter developer account, if you don't have. 1) Just navigate to Twitter Developer web...

We all know, Artificial Intelligence (AI) is one of the most transformative tech evolutions of our times. Well, if you still not aware of what AI is? It is the simulation of human intelligence in machines that are programmed to think like humans and ...

I'm a Indian and I recently moved to the USA to pursue my Master's degree at UT Arlington. So I need to carry some essential documents digitally like passport, health reports and other Government id's to be on the safer side and I don't want others t...

In Machine Learning classification problems, there are often too many factors on the basis of which the final classification is done. These factors are basically variables called features. The higher the number of features, the harder it gets to visu...

We download many files from the internet and whatever file type it may be, all files will be downloaded and located in the 'Downloads' folder. And it looks like clutter. So today we will see, how to sort files with respect to filetype extension i.e mp4, docx, pdf, exe etc And we can use a python script to sort files with respect to filetype extension
Step 1: In order to workaround we need some modules, and we can import them using 'import' keyword
Step 2: so with the help of glob module, we retrive all files present in that path location
ex: filename=glob.glob("C:/Users/Aleti Sunil/Downloads/*")
Step 3: we will create lists with respect to filetype extensions
documents=['.pdf','.docx','.doc','.txt']
media=['.jpeg','.jpg','.svg','.png','.PNG','.mp4','.mp3']
setupFiles=['.exe','.msi']
compressedFiles=['.zip']
files=['.apk']
And specify the path locations of documents,media and setupfiles respectively
DocumentsLocation='C:/Users/Aleti Sunil/Downloads/documents'
mediaLocation='C:/Users/Aleti Sunil/Downloads/media'
setupFilesLocation='C:/Users/Aleti Sunil/Downloads/setupFiles'
compressedFilesLocation='C:/Users/Aleti Sunil/Downloads/compressedFiles'
FilesLocation='C:/Users/Aleti Sunil/Downloads/Files'
Step 4: And this is the final step, we will extract the filetype extension using 'os.path.splitext' and moves the files with respect to filetype extension to specified location
And this is final code
import os
import glob
import shutil
from os import path
filename=glob.glob("C:/Users/Aleti Sunil/Downloads/*")
documents=['.pdf','.docx','.doc','.txt']
media=['.jpeg','.jpg','.svg','.png','.PNG','.mp4','.mp3']
setupFiles=['.exe','.msi']
compressedFiles=['.zip']
files=['.apk']
DocumentsLocation='C:/Users/Aleti Sunil/Downloads/documents'
mediaLocation='C:/Users/Aleti Sunil/Downloads/media'
setupFilesLocation='C:/Users/Aleti Sunil/Downloads/setupFiles'
compressedFilesLocation='C:/Users/Aleti Sunil/Downloads/compressedFiles'
FilesLocation='C:/Users/Aleti Sunil/Downloads/Files'
for file in filename:
if os.path.splitext(file)[1] in documents:
if(path.exists(DocumentsLocation)):
shutil.move(file,DocumentsLocation)
else:
os.mkdir(DocumentsLocation)
shutil.move(file,DocumentsLocation)
if os.path.splitext(file)[1] in media:
if(path.exists(mediaLocation)):
shutil.move(file,mediaLocation)
else:
os.mkdir(mediaLocation)
shutil.move(file,mediaLocation)
if os.path.splitext(file)[1] in setupFiles:
if(path.exists(setupFilesLocation)):
shutil.move(file,setupFilesLocation)
else:
os.mkdir(setupFilesLocation)
shutil.move(file,setupFilesLocation)
if os.path.splitext(file)[1] in compressedFiles:
if(path.exists(compressedFilesLocation)):
shutil.move(file,compressedFilesLocation)
else:
os.mkdir(compressedFilesLocation)
shutil.move(file,compressedFilesLocation)
if os.path.splitext(file)[1] in files:
if(path.exists(FilesLocation)):
shutil.move(file,FilesLocation)
else:
os.mkdir(FilesLocation)
shutil.move(file,FilesLocation)
Check out the video on how it works
You can also schedule this script for every regular interval
Let me know if any queries
Happy Coding โค๏ธ