Skip to content

Instantly share code, notes, and snippets.

@zet4
Last active August 29, 2015 13:57
Show Gist options
  • Save zet4/9661433 to your computer and use it in GitHub Desktop.
Save zet4/9661433 to your computer and use it in GitHub Desktop.
You will need python 3 or newer to run this script, save as anything you want in the folder to clean, run, enjoy the result.
import os
#Check and/or create directory 'dirname'.
def make_dir(dirname):
if os.path.isdir(dirname) != True:
os.mkdir(dirname)
#Check and/or rename file 'fr' to 'to'
def move_file(fr, to):
if os.path.isfile(to) != True:
os.rename(fr, to)
#Get current execution location.
take = os.path.dirname(os.path.abspath(__file__))
#Base folder for 'trash'.
give = os.path.join(take, "cleanedUp")
make_dir(give)
#Remove if wanted, not necesery. {
give = os.path.join(give, os.path.basename(take))
make_dir(give)
#}
#Simply for luxary and information.
print("Hello user, would you like to clean your desktop?")
print("Well ofcourse you do, why else would you run this script, now please wait.")
print("Cleaning", take)
#Traverse through 'take' directory.
for name in os.listdir(take):
if (os.path.isdir(name) != True) and (name != os.path.basename(__file__)):
splited = name.split('.')
#Move the files.
#In case the file dosen't have extension, use 'typeless' word for folder.
if len(splited) == 1:
make_dir(os.path.join(give, "typeless"))
move_file(os.path.join(take, name), os.path.join(give, "typeless", name))
else:
make_dir(os.path.join(give, splited[len(splited)-1]))
move_file(os.path.join(take, name), os.path.join(give, splited[len(splited)-1], name))
#Output the data.
print(name, "Moved to " + os.path.join(give, splited[len(splited)-1], name))
@rasbt
Copy link

rasbt commented Mar 20, 2014

Nice, made some revisions in my fork if you like to merge them! (https://gist.github.com/rasbt/9669548/f0e3df06c2fab8d5d230e16612e2adbe24f02e45)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment