Python Script To Clean Desktop.
Join the DZone community and get the full member experience.
Join For FreeThis little script cleans my desktop.
โ
โ
from os import listdir as ls, mkdir, remove as rm
from os.path import join, isdir, exists
from shutil import copyfile as copy
โ
"""
I always downloads pictures and documents directly onto my desktop,
as a result my desktop turns out to look like a recycle bin.
This script helps me to organize these small files into directories according to their types.
@author : PhoenixR
"""
โ
exts = {'jpg' : 'Pictures', 'png' :'Pictures', 'gif' :'Pictures',
'pdf' : 'Documents', 'doc':'Documents' , 'ppt' : 'Documents'}
โ
def get_type(file) :
if exts.has_key( file[-3:] ) :
return exts[file[-3:]]
else :
return None
โ
def new_dir( path, file, type=None) :
"""
Make a new directory is the file type hasn't been recognized.
Copy the file to the new directory, and remove the original copy.
"""
if type is None :
return
if not exists( join(path, type) ) :
mkdir( join(path, type) )
copy( join(path, file), join( path, join(type, file) ))
print 'File %s is deleted' % join(path, file)
rm( join(path,file) )
def main(root=None) :
if root is None :
path = '.'
else :
path = root
files = ls(path)
for file in files :
if not isdir( join( path, file) ) :
new_dir( path, file, get_type( file) )
if __name__ == '__main__' :
main()
โ
Opinions expressed by DZone contributors are their own.
Comments