Junk File Organizer in Python

Basically, as a lazy programmer, my desktop is full of Junk Files. Due to the large number of files, it is difficult to sit and organize each file. To make this easier, the below Python script comes in handy and all files are organized within a few seconds.

Screenshot before running script


Here are the steps to follow:

  1. Create dictionaries: the code below will create specific directories.
     DIRECTORIES = {"HTML": [".html5", ".html", ".htm", ".xhtml"], "IMAGES": [".jpeg" , ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", "svg", ".heif", ".psd"], "VIDEOS": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".vob", ".mng", ".qt", ".mpg" , ".mpeg", ".3gp"], "DOCUMENTS": [".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf", ".ods "," .odt "," .pwi "," .xsn "," .xps "," .dotx "," .docm "," .dox "," .rvg "," .rtf "," .rtfd "," .wpd "," .xls "," .xlsx "," .ppt "," pptx "]," ARCHIVES ": [" .a "," .ar "," .cpio "," .iso "," .tar "," .gz "," .rz "," .7z "," .dmg "," .rar "," .xar "," .zip "]," AUDIO ": [ ".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3", ".msv", "ogg", " oga "," .raw "," .vox "," .wav "," .wma "]," PLAINTEXT ": [" .txt "," .in "," .out "]," PDF ": [ ".pdf"], "PYTHON": [".py"], "XML": [".xml"], "EXE": [".exe"], "SHELL": [".sh"]} 
  2. Matching: we will now match file formats to the directory.
     FILE_FORMATS = {file_format: directory for directory, file_formats in DIRECTORIES.items () for file_format in file_formats} 

    Here we map file extensions to a directory.

     def organize_junk (): for entry in os.scandir (): if entry.is_dir (): continue file_path = Path ( entry) file_format = file_path.suffix.lower () if file_format in FILE_FORMATS: directory_path = Path (FILE_FORMATS [file_format]) directory_path.mkdir (exist_ok = True) file_path.rename (directory_path.joinpath (file_path)) for dir in os.scandir (): try: os.rmdir (dir) except: pass 

    The above function will check the existing directory for the same name we defined shared. If an existing directory is found, it will continue or a new directory will be created. And it will categorize all files based on extension in the appropriate folder.

  3. Organization: Below is the code for Python Lazy Junk Files Organizer. It organizes everything in the appropriate folder in one go and removes empty directories.

    import os

    from pathlib import Path

     

    DIRECTORIES = {

      " HTML " : [ ". Html5 " , ". html" , ". htm" , ". xhtml " ],

      " IMAGES " : [ ". jpeg" , ".jpg" , ". tiff" , ". gif" , ". bmp" , ". png " , ". bpg " , "svg" ,

    ". heif" , ". psd" ],

    "VIDEOS" : [ ". Avi" , ". flv" , ". wmv" ". mov" , ". mp4" , ". webm" , " .vob " , ". mng " ,

    ". qt" , ". mpg" , ". mpeg" , ". 3gp" ],

    " DOCUMENTS " : [ ". oxps " , ". epub" , ". pages" , ". docx" , ". doc" , ". fdf" , ". ods" ,

      ". odt" , ". pwi" , ". xsn" , ".xps" , ". dotx" , ". docm" , ". dox" ,

      ". rvg " , ". rtf " , ". rtfd " , ". wpd" , ". xls "< / code> , ". xlsx" , ". ppt " ,

      " pptx " ],

      "ARCHIVES" : [ ". A" , ". ar" , ". cpio" , ". iso" , ". tar" , ". gz" , ". rz" , ". 7z" ,

    ". dmg" , ". rar" , ". xar" , ". zip" ],

    " AUDIO " : [ ". aac " , ". aa" , ". aac" , ". dvf" , ". m4a" , ". m4b" , ". m4p" , ". mp3" ,

    ". msv" , "ogg" , " oga " , ". raw" , ". vox " , ". wav " , ". wma" ],

    "PLAINTEXT" : [ ". txt" , ". in" , ". out " ],

      " PDF " : [ ". pdf" ],

    "PYTHON" : [ ". py" ],

    " XML " : [ ". xml " ],

    "EXE" : [ ". exe" ],

    "SHELL" : [ ". sh" ]

     
    }

     

    FILE_FORMATS = {file_format: directory

    for directory, file_formats in DIRECTORIES.items ()

    for file_format in file_formats}

     

    def organize_junk ():

    for entry in os.scandir ():

    if entry.is_dir ():

      continue

      file_path = Path (entry)

    file_format = file_path.suffix.lower ()

      if file_format in FILE_FORMATS:

    directory_path = Path (FILE_FORMATS [file_format])

    directory_path.mkdir (exist_ok = True )

    file_path.rename (directory_path.joinpath (file_path))

      

      for dir in os.scandir ():

      try :

      os.rmdir ( dir )

    except :

    pass

     

    if __ name__ = = "__ main__" :

    organize_junk ()

    Save the above script as orgjunk.py ... For example, you want to organize your files on your desktop, then copy and paste the orgjunk.py file on your desktop and run.

  4. Screenshot after running the script

This article is courtesy of Srce Cde . If you like Python.Engineering and would like to contribute, you can also write an article using contrib.python.engineering, or email your article to [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.