In this article, we are going to know how to automatically organize the download folder in Python.
Are you a disorganized person who just keeps on downloading the files and does not organize them? Then, you must definitely check out this article as in this article, we will be discussing how to automatically organize the downloads folder in Python. What you need to exactly do is obtain the path of the downloads folder, then extract the file type from the file name. Once extracted file type, create the new subfolders inside the downloads folder and move the files from the source path to the new subfolders created now.
Stepwise Implementation:
Step 1: Import the libraries
First of all, we import the libraries, os, and shutil. The os library provides a portable way of using operating system-dependent functionality, while shutil library offers a number of high-level operations on files and collections of files.
from os import listdir from os.path import isfile, join import os import shutil
Step 2: Obtain the path of the folder to be organized
In this step, we will get the path of the folder, i.e. the downloads folder.
file_path='D:\Downloads'
Step 3: Obtain all the files from the path in the list
In this step, we have used them for loop to get all the file names in the list.
files = [f for f in listdir(file_path) if isfile(join(file_path, f))]
Step 4: Create the empty list and dictionary
Now, we will create an empty list for storing the file type, while an empty dictionary for storing the new folder names being created.
file_list=[] filetype_dict={}
Step 5: Create a loop for all the files
Next, we run a loop for reading all the file names one by one.
for file in files:
Step 5.1: Extract the file type from the file name
Further, we obtain the file type from the name by file by splitting the file name by dot and obtaining the part after the dot.
filetype=file.split('.')[1]
Step 5.2: Check if the file type exists in the list
In this step, we will check if the file type extracted from the file name exists in the list or not.
if filetype not in file_list:
Step 5.2 (a): Add the file type in a list if not already there, Later on, append the file type in the list if it does not exist there.
file_list.append(filetype)
Step 5.2 (b): Give naming to the newly created folders
In this step, we create names for the sub-folders inside the downloads folder.
new_folder_name=file_path+'/'+ filetype + '_folder'
Step 5.2 (c): Add the new folder name in the dictionary with the key-value pairs
Moreover, we add the new folder names created in the last step with the file type as key-value pairs in the dictionary.
filetype_dict[str(filetype)]=str(new_folder_name)
Step 5.2 (d): Check if the sub-folder exists or not
In this step, we check if the sub-folder with the name we created in the last step exists or not. If it exists, then come out of the loop.
if os.path.isdir(new_folder_name)==True: continue
Step 5.2 (e): Create a new sub-folder if the sub-folder does not exist
However, if the sub-folder does not exist, we create a new sub-folder for organizing the files.
else: os.mkdir(new_folder_name)
Step 6: Declare a new variable
Next, we declare a new variable, i.e. i, with the value 1.
i=1
Step 7: Create a loop for all the files
Next, we run a loop for reading all the file names one by one.
for file in files:
Step 7.1: Obtain the source path of each file
Now, we create a source path for each file by adding the file name to the path obtained from the user.
src_path=file_path+'/'+file
Step 7.2: Extract the file type from the file name
Further, we obtain the file type from the name by file by splitting the file name by dot and obtaining the part after the dot.
filetype=file.split('.')[1]
Step 7.3: Check if the file type exists in the dictionary
In this step, we will check if the file type extracted from the file name exists in the dictionary or not.
if filetype in filetype_dict.keys():
Step 7.3 (a): Store the path of the new folder in the new variable
Next, we store the path of the new folder from the dictionary in the new variable titled dest_path.
dest_path=filetype_dict[str(filetype)]
Step 7.3 (b): Move the file from the source path to the destination path
In this step, we finally move the file from the user-defined path to the new sub-folders created by us.
shutil.move(src_path,dest_path)
Step 7.4: Print from where to where a file is being moved
Moreover, we print the path from which the file exists earlier to the final path the file is being moved.
print(i,'. ',src_path + '>>>' + dest_path)
Step 7.5: Increment the value of the variable declared
Finally, we increment the value of the variable, i.e. i by one.
i=i+1
Example:
Python
# Python program to automatically organize # Downloads folder in Python # Import the libraries from os import listdir from os.path import isfile, join import os import shutil # Obtain the path to be organized file_path = 'D:\Downloads' # Obtain all the files from the path in list files = [f for f in listdir(file_path) if isfile(join(file_path, f))] # Create the blank list and dictionary file_list = [] filetype_dict = {} # Create a loop for file in files: # Split the name of file from dot filetype = file .split( '.' )[ 1 ] # Check if the file type exists in the list if filetype not in file_list: # Add the file type in list if not already there file_list.append(filetype) # Give naming to the newly created folders new_folder_name = file_path + '/' + filetype + '_folder' # Add the new folder name in dictionary with the key value pairs filetype_dict[ str (filetype)] = str (new_folder_name) # Check if the folder exists or not if os.path.isdir(new_folder_name) = = True : # Come out of the loop if folder exists continue else : # Create the new folder if does not exist os.mkdir(new_folder_name) # Declare a variable with value 1 i = 1 # Create the loop for all the files for file in files: # Get the source path of each file src_path = file_path + '/' + file # Split the name of file by dot filetype = file .split( '.' )[ 1 ] # Check if the file type exists in the dictionary if filetype in filetype_dict.keys(): # Add the file type in dictionary if not already there dest_path = filetype_dict[ str (filetype)] # Move the file from source path to destination path shutil.move(src_path, dest_path) # Print from where to where a file is being moved print (i, '. ' , src_path + '>>>' + dest_path) # Increment the value of variable by 1 i = i + 1 |
Output: