Introduction of the Project
Are you lazy in terms of organizing your files in your system, or do you like to organize your files using some automation? In either case, this script will help you perform organizational tasks in your system. All you need is to run the script in that particular folder, and all the files will be grouped wrt to their extensions. Seems useful, right? So, today, we will create Junk File Organizer in Python with OS Module to successfully do efficient organization of files with a smaller piece of code.
1. Python 3.9 interpreter and IDLE (online or system configured)
2. OS and shutil modules must be installed on your pc.
3. Tkinter module.
Steps To Create Junk File Organizer In Python With OS Module
Step 1: Perform the following as per Operating System:
Windows: Open Command Prompt and type
macOS: Open Terminal and type
pip install os
pip install tk
Installing os and Tkinter
Step 2: Copy the below piece of code in your editor/IDE.
# Import modules from tkinter import * from tkinter import filedialog import os import shutil # Class for GUI class GUI(Tk): # To set the dimensions of the window def __init__(self): super().__init__() self.geometry('400x200') # To get the path, Ask for a directory, and return the file name def gettingPath(self): self.path = filedialog.askdirectory() return self.path # Construction of a button to start the operation def startButton(self, path_value): self.button_Frame = Frame(self) self.button_Frame.pack() self.start_Button = Button(self.button_Frame, text='Start', command=lambda: self.startOperation(path_value)).grid(row=0, column=0) # To organize files def startOperation(self, path_value): count = 0 os.chdir(path_value) self.file_list = os.listdir() no_of_files = len(self.file_list) # To check if the folder is empty if len(self.file_list) == 0: self.error_label = Label(text="Error!! Empty folder").pack() exit() for file in self.file_list: # To create a folder for PNG extension images if file.endswith(".png"): self.dir_name = "PNGFiles" self.new_path = os.path.join(path_value, self.dir_name) self.file_list = os.listdir() if self.dir_name not in self.file_list: os.mkdir(self.new_path) shutil.move(file, self.new_path) # To create a folder for txt extension files elif file.endswith(".txt"): self.dir_name = "TextFiles" self.new_path = os.path.join(path_value, self.dir_name) self.file_list = os.listdir() if self.dir_name not in self.file_list: os.mkdir(self.new_path) shutil.move(file, self.new_path) count = count+1 if(count == no_of_files): success = Label(text="Files organized Successfuly!").pack() else: error = Label(text="Failed").pack() # To run the application if __name__ == '__main__': object = GUI() path = object.gettingPath() object.startButton(path) object.mainloop()
Explanation Of The Code
In the beginning, we imported the os, shutil, and Tkinter modules.
1. Inside the class of GUI, we have defined functions in which the first function we have set the dimensions of the window.
2. The second function is to get the path, Ask for a directory, and return the file name.
3. Now, we have constructed a button that will mark the operation’s start.
4. In the next function, we are performing our main tasks of organizing files wrt their extensions and adding them to a subfolder wrt their extensions.
5. At last, we are running our application.
The below GUI shows the output message after the successful organization of the files.
Things to Remember
To ensure the successful outcome of this python code to create a Junk File Organizer In Python With OS Module, keep the following points in mind.
- Choose the folder in which you need to organize files and make a subfolder with the same extension files.
- The process is not easily reversible so do it cautiously.
Cisco Ramon is an American software engineer who has experience in several popular and commercially successful programming languages and development tools. He has been writing content since last 5 years. He is a Senior Manager at Rude Labs Pvt. Ltd.