Change language

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.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically