Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to create file organizer using python

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", ".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()
Comment

PREVIOUS NEXT
Code Example
Python :: get linkinstance revit api 
Python :: Return an RDD with the keys of each tuple. 
Python :: Let’s add a docstring to the greeting method. How about, “Outputs a message with the name of the person”. 
Python :: stackoverflow - import data on colabs 
Python :: py to flag converter online 
Python :: AGE CALCULATOION IN PYTHON 
Python :: Write a Python program to accept two strings as input and check if they are identical copy of each other or if the second string is a substring of the first string. 
Python :: draw a bow tie in python 
Python :: Use in in django while preserving order 
Python :: python enforcing class variables in subclass 
Python :: queryset o que é 
Python :: roll a dice 
Python :: python coding for y, you will also display a “bar” of ‘X’ characters to represent the number. For example, the prime number 2 would be represented as “X 2”. 
Shell :: git ignore permission changes 
Shell :: how to check mongodb status in ubuntu 
Shell :: conda install gensim 
Shell :: Zsh is not installed. Please install zsh first. 
Shell :: test internet speed terminal linux 
Shell :: how to install yum in ubuntu 
Shell :: how to open xampp control panel in ubuntu 
Shell :: Reset git local branch to remote branch 
Shell :: git command show current repo 
Shell :: install wps ubuntu 20.04 multilanguage 
Shell :: nonexistentpath data directory /data/db not found 
Shell :: npm install --global yarn 
Shell :: git username 
Shell :: zsh: command not found: gatsby 
Shell :: how to update laravel installer 
Shell :: ps1 file not digitally signed 
Shell :: xampp starting apache...fail. ubuntu 20.04 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =