DEV Community

Cover image for Desktop organization with Python
Stokry
Stokry

Posted on

Desktop organization with Python

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()

Enter fullscreen mode Exit fullscreen mode

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

Top comments (15)

Collapse
 
akshay090 profile image
Akshay090

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?

Collapse
 
stokry profile image
Stokry

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.

Collapse
 
bionboy profile image
Luke Floden

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

Collapse
 
stokry profile image
Stokry

Yeah, thanks for the tip!

Collapse
 
bionboy profile image
Luke Floden

np! great script!

Collapse
 
narasimha1997 profile image
Narasimha Prasanna HN • Edited

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

Collapse
 
marcaube profile image
Marc Aubé

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!

Collapse
 
stokry profile image
Stokry

Nice, I will try this, thank you.

Collapse
 
jldohmann profile image
Jesse

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

Collapse
 
stokry profile image
Stokry

Thank you, this module is very powerful!

Collapse
 
pablohs1986 profile image
Pablo Herrero

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

Collapse
 
stokry profile image
Stokry

Thank you.😀

Collapse
 
andrewbaisden profile image
Andrew Baisden

Impressive I did not know about this.

Collapse
 
hectorpascual profile image
Héctor Pascual

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 !

Collapse
 
stokry profile image
Stokry

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