Automatically organize downloads folder in Python
Last Updated :
01 Aug, 2022
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:
Similar Reads
Download a file over HTTP in Python
In this article, we are going to see how to install a File from HTTP using Python. For this we will use the following methods: Using RequestUsing urllibUsing wgetUsing requests Here we send an HTTP request to the server and save the HTTP response in a response object and received content as a .png f
1 min read
Raise a File Download Dialog Box in Python
Raising a File Download Dialog Box for end-user to download files like pdf, media-objects, documents, etc in Python can be done by the use of HTTP Header. It comes in handy where there is a need to develop a feature where instead of showing the files in the browser, the file contains is automaticall
2 min read
Making automatic module installer in Python
Prerequisites: urllibsubprocess Many times the task of installing a module not already available in built-in Python can seem frustrating. This article focuses on removing the task of opening command-line interface and type the pip install module name to download some python modules. In this article,
2 min read
How to Download All Images from a Web Page in Python?
Prerequisite: Requests BeautifulSouposFile Handling Web scraping is a technique to fetch data from websites. While surfing on the web, many websites donât allow the user to save data for personal use. One way is to manually copy-paste the data, which both tedious and time-consuming. Web Scraping is
3 min read
How To Download Folder From AWS S3 CLI & UI ?
The AWS Simple Storage Service (S3) is a cloud service provided by Amazon Web Services (AWS) to store your data securely. There are different approaches to storing and retrieving data from AWS S3; one of them is by using AWS CLI provided by Amazon Web Services. In this article, we will provide you w
4 min read
How to Find Downloads on a Mac?
Finding downloads on a Mac might be useful whether you've recently downloaded an item or are looking for an older download. While downloaded files on macOS normally end up in a default folder, they can occasionally be hard to find. This article will walk you through accessing downloads on a Mac, cov
3 min read
Junk File Organizer in Python
Basically, as a lazy programmer, my desktop is full of files (Junk Files). Due to a large number of files, it is a daunting task to sit and organize each file. To make that task easy the below Python script comes in handy and all the files are organized in a well-manner within seconds. Screenshot be
3 min read
Where to find the downloads folder on PC, Mac, iPhone, iPad & Android
Where to find the downloads folder on PC, Mac, iPhone, iPad & AndroidDownloading our favorite movies, series, or any important document, images, or videos for any critical task is one of our everyday tasks. But sometimes, we need help to relocate our downloaded files. Suppose you are stuck in an
7 min read
Change the Download Location in Google Chrome
Have you ever found yourself searching for a downloaded file in Google Chrome, only to realize it's stored in a location you didn't expect? If you're tired of sifting through endless folders to find your downloads, you're not alone. Changing the chrome download location is a quick fix to make your b
4 min read
Python - Move and overwrite files and folders
In this article, we will be learning on moving a collection of files and folders where there may be files/folders with the same name as in the source name in the destination. So that we may need to overwrite the existing destination file with the source file. The shutil.move() method is used to move
3 min read