Head of Digital Marketing @Bronco. Lover of Python and Data.

# Keeping Your Desktop Tidy - Like A Boss!

I'm still on my path to Pythonic enlightenment and today on my break I wrote a little script that I'm now going to be using to keep my desktop tidy and my files well organised. Basically I'm terrible for saving my files to my desktop; not only is this messy and unorganised, it's dangerous as if my computer dies, my work might with it - so my files should be stored on the company's backed-up common drive.

When I save files I always start them with the clients name followed by a brief description; for example, client_name_link_analysis.csv, due to this I was able to use it to my advantage in letting a script organise things for me.

Here it is...

Note!! There is a newer version at the end of this post!

import glob
import os
import shutil

os.chdir("d:\Desktop")

other_folder = "d:\Desktop\other"

for files in glob.glob("*.*"):
x = str(files).lower()
if 'INSERT CLIENT NAME IN THE FILE NAME' in x:
print x
shutil.move(x, client_folder1)
elif 'INSERT CLIENT NAME IN THE FILE NAME' in x:
print x
shutil.move(x, client_folder2)
elif 'INSERT CLIENT NAME IN THE FILE NAME' in x:
print x
shutil.move(x , client_folder3)
elif 'filemove' in x:
pass
else:
print x
shutil.move(x , other_folder)


So lets break it down a little bit!

The following are the imports that we need to run some of the functions, these are just part of the standard library so no special installs here - Just install python and create a new file from IDLE, make the necessary changes and double click it to run it (just like you do to open any other program).


import glob
import os
import shutil


This next bit you don't actually need but it just depends how you are going to use it. Basically this changes the current working directory to the desktop, i'm saying you probably don't need it as you'll just have this script sat on your desktop ready to run. It's only needed if you wanted to clean it while the program was sat in another directory!


os.chdir("d:\Desktop")


Ok so the next lines are for your destination folder locations, wherever they my be, just paste in the folder locations like how I have and assign new variables too if you'd like. Note: if you change the variables i.e. "client_folder1" to your actual clients name then make sure you change everywhere else in the program too.


other_folder = "d:\Desktop\other"


The next line is using the glob function; glob.glob("*.*") the two * are wild cards, so if you only wanted to find .txt files you would go glob.glob("*.txt"). So the first asterix is the file name and the second is the file extensions, we're using two wildcards as we want all files to be checked and moved. This is also starting a for loop which is use to reiterate over all files and starts a series of changes and checks against the files...


for files in glob.glob("*.*"):


This next line is apart of the above for loop and it's basically saying turn the file names into a string str() (aka text) and make them lowercase .lower() This is so it's readable no matter how you write it in the file i.e. client_link_analysis.csv Vs. CLIENT_link_analysis.csv.

x = str(files).lower()


So this next section of code continues with the reiteration; it's basically saying if the name of the client is within the file name do this... it prints it to the screen and then moves it to the destination folder (the print was really just for testing and so you know things have moved). shutil.move(x, client_folder1) is the function that does the moving, x is the file and client_folder1 is the variable that is assigned the destination folder path as above.

This then carries on for different conditions, if you want to add or remove conditions use the elif section to do this!

if 'INSERT CLIENT NAME IN THE FILE NAME' in x:
print x
shutil.move(x, client_folder1)
elif 'INSERT CLIENT NAME IN THE FILE NAME'' in x:
print x
shutil.move(x, client_folder2)


The last two blocks of code are doing the following; the elif is basically telling the program to ignore itself, otherwise it will shoot off to the 'other' folder which I'll get to in just a second. If you come up with a more creative name for the file, don't forget to change it here.

The else statement is saying everything else that doesn't match these conditions move to the 'other' folder. This is just odds and ends really but obviously just adjust to suit your needs.

elif 'filemove' in x:
pass
else:
print x
shutil.move(x , other_folder)


So that's it. Obviously it needs changing to what you need it for, my actual working script looks quite different to this but this is the basic workings of it. Any questions let me know - oh and share this if you like it!!!

UPDATE.

I have rewritten it to be more maintainable, thanks to advice from Erwin de Keijzer

import glob
import os
import shutil

# specifies the desktop
os.chdir("d:\Desktop")

# client names and folder locations - used python extension as an example
python = {'.py':'d:\Desktop\Python Scripts'}

# loops over files and checks against dictionary keys and moves to dictionary values (folder paths)
# The 'if' is to ignore this program so it isn't moved also.
for files in glob.glob("*.*"):
x = str(files).lower()
for client, folder in python.items():
if 'filemove' in x:
pass
elif client in x:
print x
shutil.move(x, folder)


Now to add new clients into the mix I just update the dictionary with the client name and folder locations and nothing else, this will make it cleaner and easier to update and maintain.

### Give Your Inbox Some Love

What You'll get?
• Posts like this delivered straight to your inbox!
• Nothing else.
Comment Policy

Any code snippets more than a line or 2, please include as a link to a gist