Python script for Sorting Files w.r.t Filetype Extensions
Tags: python, productivity, tutorial
Published: 2020-04-24 10:48:52

{%github https://github.com/aletisunil/sort-files-with-same-extension no-readme %}
We download many files from internet and whatever file type it may be, all files will be downloaded and located in 'Downloads' folder. And it looks like a 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 python script to sort files with respect to filetype extension

<b>Step 1:<b>
  In order to workaround we need some modules, and we can import them using <b>'import'</b> keyword
* os     // allows you to interface with the underlying operating system
* shutil // responsible for copying nd moving files respectively
* glob   // used to retrieve files/pathnames matching a specified pattern

<b>Step 2:</b>
  so with the help of glob module, we retrive all files present in that pathlocation
```python
ex: filename=glob.glob("C:/Users/Aleti Sunil/Downloads/*")
```
<b>Step 3:</b>
we will create lists with respect to filetype extensions
```python
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
```python
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'
```

<b>Step 4:</b>
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
```python
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)
```
A demo video on how it works
{% vimeo https://vimeo.com/411362595 %}

Let me know, if any queries
Happy Coding ❤️