Clean your Desktop using python – Python project

Clean your Desktop using python – Python projects

Hello guys! welcome to an amazing python tutorial. This is a simple python project,but you definitely you love it. I am sure you definitely love it.

General Problem

If you are using PC or laptop, we always have habit of making a clumsy desktop or fill up folders with multiple files.

we download or create music, videos, images, pdf, text, and zip files in desktop or in folders. It looks really ugly and sometimes it irritates us. we don’t have time to clean it right! If we did once, again we do the same things in folders or desktop.

Solution

How do we resolve this issue..? Here is our HERO.

Yes, Python solves this issue. I used a python script that separates out the type of files like .mp4 or mp3 or .txt or .pdf and moves them into their corresponding folders.

I know you are so excited to know how it works and how write this script using python. I will give you entire script at the end of tutorial,before that you need some basics of python modules.

What you need in this python project

Luckily you don’t need any external libraries or packages to accomplish this project. The only thing is here, you must install python in your system. if you don’t download it here.

I used two python modules in this project. They are

  • os
  • shutil

Os module:

It is used for basic operations in a computer like creating a new folder and getting current working directory and file checking etc.

shutil module:

It is used for high-level operations like moving a file from one path to another path, to know more read the doc.

Read My best python tutorial

Download music using python

How to trace mobile number using python

How to send emails using Python

The things you must know

To accomplish this python project you must know some important things, they are below

  • Creating folders using OS
  • try and except blocks
  • if-else statements
  • move files using shutil

Creating folders using OS

This is the basic thing and important thing in this project. here is the code to create a new folder using python os module.

import os

os.mkdir("Folder Name")

That’s it. Its very simple isn’t?

try and except blocks

we must know whether the given folder is created before or exists otherwise it will throw an error. The try and except block help us to avoid these errors. see how it works.

a =10

try:
    print(a+hello) # we can't concatenate a string and int in python.

except:
    print("sorry I can't") 

if – elif -else condition:

Don’t get angry on me ! This is for complete beginners guys.! I know you these things, but a little from my side.

my_color ='green'

if my_color =='yellow':
   print('yellow')
elif my_color =='red':
     print("red")
else:
    print('I dont know')

Note: if or elif execute only when they are True.

Moving files using shutil

This is another important thing in our project, we use this method to move our images or music to their corresponding folders.

import os
import shutil


curr_path = os.path.join(os.getcwd(),file)
dest_path = os.path.join(os.getcwd(),'Pictures')

shutil.move(curr_path,dest_path)

That’s it guys, these are enough to build this amazing python project. here is my code ,it helps you a lot.

Happy coding!

automate.py

import os
import shutil


#print(os.listdir())



try:
    os.mkdir('Pictures')
    
except:
    pass

try:
    os.mkdir('Folders')
    
except:
    pass

try:
    os.mkdir('videos')
    
except:
    pass
try:
    os.mkdir('Docs')
    
except:
    pass



for file in os.listdir():
    
    if '.mp3' in file:
        try:
            curr_path = os.path.join(os.getcwd(),file)
            dest_path = os.path.join(os.getcwd(),'Music')
            shutil.move(curr_path,dest_path)
        except:
            pass
       
    elif '.jpg' in file or '.png' in file:
        try:
            curr_path = os.path.join(os.getcwd(),file)
            dest_path = os.path.join(os.getcwd(),'Pictures')
            shutil.move(curr_path,dest_path)
        except:
            pass
        
    elif '.zip' in file or '.rar' in file:
        try:
            curr_path = os.path.join(os.getcwd(),file)
            dest_path = os.path.join(os.getcwd(),'Folders')
            shutil.move(curr_path,dest_path)
        except:
            pass
    
    elif '.mp4' in file:
        try:
            
            curr_path = os.path.join(os.getcwd(),file)
            dest_path = os.path.join(os.getcwd(),'videos')
            shutil.move(curr_path,dest_path)
        except:
            pass
    
    elif '.pdf' in file or '.txt' in file:
        try:
            
            curr_path = os.path.join(os.getcwd(),file)
            dest_path = os.path.join(os.getcwd(),'Docs')
            shutil.move(curr_path,dest_path)
        except:
            pass
            
        
        
     

Create a new python file and copy the code and paste it into your file and finally save it. Now you are ready to get the fruits of this script.

Copy this python file wherever you need to clean your folders or maybe desktop and run the python file. It just takes seconds to clean your desktop or folders and move the files to corresponding folders.

Python do everything for you, you just sit and RELAX !

Its not the end

I gave the clear picture of it, now you need to avoid the creation of unnecessary folders. Please try to implement it, that will give more satisfaction to you, in fact that’s the habit of learning and building.

I hope you like this python tutorial, subscribe to our newsletter to get more amazing python tutorials for free and follow us on Facebook. Thank you!

#Happycoding

Leave a Reply