#!/usr/bin/env python import os, sys,time,shutil from os.path import join ## --------------------------------------------------- ## -- picture organizer ## organizes files into seperate directories ## ## -- author: Patrick Lacson (placson at yahoo dot com) ## --------------------------------------------------- def usage(): print """ Usage: organizer (input directory) (output directory) [OPTIONS] -m move the files. (Copies by default) """ sys.exit(-1) def organize(src,dest,doMove=False): #time_fmt = "%m_%d_%y" # better sort -- Nathan Chilton nathanchilton@yahoo.com 02/24/05 time_fmt = "%y%m%d" total_files = 0 if not os.access(src,os.F_OK): # check for existence print 'Source directory %s, does not exist!' % (src) usage() if not os.access(dest,os.F_OK): # check for existence print 'Destination directory %s, does not exist!' % (dest) usage() for root, dirs, files in os.walk(src): for name in files: ts = os.stat(join(root,name)).st_mtime time_str = time.strftime(time_fmt, time.gmtime(ts)) total_files += os.stat(join(root,name)).st_size try: srcfile = join(root,name) dir2create = join(dest,time_str) if not os.access(dir2create,os.F_OK): os.mkdir(dir2create) if doMove: print 'moving %s to %s ... ' %(srcfile,dir2create) shutil.move(srcfile,dir2create) else: print 'copying %s to %s ... ' %(srcfile,dir2create) shutil.copy2(srcfile,dir2create) except OSError, (errno,strerror): print 'Error making directory: %s,(%s)' %(dir2create,strerror) sys.exit(-1) except IOError, (errno,strerror): print strerror sys.exit(-1) if doMove: print 'total: %d bytes moved' % (total_files) else: print 'total: %d bytes copied' % (total_files) if __name__ == '__main__': if (len(sys.argv) < 3) or len(sys.argv) > 4: usage() elif (len(sys.argv) == 4): organize(sys.argv[1],sys.argv[2],True) else: organize(sys.argv[1],sys.argv[2])