loading...
Cover image for Desktop organization with Python

Desktop organization with Python

stokry profile image Stokry ・1 min read

Today I will show you how to build a simple desktop organization script using the watchdog module, which observes the Desktop for any changes.
The basic idea is when you put a file on a desktop it will automatically be deleted and moved to a given folder.

import watchdog.events
import watchdog.observers
import shutil

class Handler(watchdog.events.PatternMatchingEventHandler):
    def __init__(self):
        watchdog.events.PatternMatchingEventHandler.__init__(self, patterns=['*.txt', '*.png', '*.jpg'], ignore_patterns = None,
                                                     ignore_directories = False, case_sensitive = True)

    def on_created(self, event):
        print(f"Created at {event.src_path}")
        if event.src_path.endswith('.txt'): 
            shutil.move(event.src_path, r'C:\Users\Stokry\Desktop\Text_Documents')          
        elif event.src_path.endswith('.png') or event.src_path.endswith('.jpg'): 
            shutil.move(event.src_path, r'C:\Users\Stokry\Desktop\Image_docs')              
    def on_deleted(self, event):
        print(f"Deleted at {event.src_path}")

event_handler = Handler()
observer = watchdog.observers.Observer()
observer.schedule(event_handler, r'C:\Users\Stokry\Desktop', recursive = False)
observer.start()
observer.join()

This is a very simple way to do it, you can build a more complex script.

Posted on by:

stokry profile

Stokry

@stokry

UX developer who falls in love with Python code and advanced algorithms.

Discussion

pic
Editor guide
 

It's really nice, thanks for sharing it.
I got an idea to implement this for a different use case, to manage the downloads folder. When you download a file it would ask whether you want to delete it after an hour or move to different location.
What do you think about this idea?

 

This is a really, really good idea. I just started something similar, for desktop. We all know that sometimes desktop can be a little bit messy so I working on a script that will automatically delete all files (on the last day of the month) that are not inside of any given folder. That is just for the organization's purpose.

 

You could make C:\Users\Stokry\Desktop into a variable

 

Yeah, thanks for the tip!

 

np! great script!

 

Just another idea, you can create a simple secure vault using this technique. You create a folder called "vault" and whatever goes inside it will be automatically encryped using a key which is predefined. Haha

 

I like automating these kinds of tasks too! I run github.com/tfeldmann/organize in a cron job with a few rules/tasks very similar to yours. I'll have to add watchdog to my arsenal!

 

Nice, I will try this, thank you.

 

Really cool, didn't know about this module! Thanks for the info

 

Thank you, this module is very powerful!

 

It's great!! I have to try it. Thank you!

 

Thank you.😀

 

Impressive I did not know about this.

 

Is the program always running on a thread until you CTRL+C the execution? Or it ends automatically.

But cool, I didn't know about this lib !

 

Hello, the program will always run until you stop it (ctrl + c).