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
from os import listdir
from os.path import isfile, join
import os
import shutil
file_path = 'D:\Downloads'
files = [f for f in listdir(file_path) if isfile(join(file_path, f))]
file_list = []
filetype_dict = {}
for file in files:
filetype = file .split( '.' )[ 1 ]
if filetype not in file_list:
file_list.append(filetype)
new_folder_name = file_path + '/' + filetype + '_folder'
filetype_dict[ str (filetype)] = str (new_folder_name)
if os.path.isdir(new_folder_name) = = True :
continue
else :
os.mkdir(new_folder_name)
i = 1
for file in files:
src_path = file_path + '/' + file
filetype = file .split( '.' )[ 1 ]
if filetype in filetype_dict.keys():
dest_path = filetype_dict[ str (filetype)]
shutil.move(src_path, dest_path)
print (i, '. ' , src_path + '>>>' + dest_path)
i = i + 1
|
Output:
Last Updated :
01 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...