以下に改良版 Python スクリプトを示します。

001:     #! /usr/bin/env python
002:     
003:     """
004:     script to achive photos in the removal media into HD
005:     by T.Shido
006:     
007:     0.1: January 12, 2005
008:     0.2: September 22, 2006
009:     0.2.1: September 26, 2006
010:     """
011:     
012:     import glob, os, os.path, shutil, filecmp, re, sys, operator
013:     from datetime import date
014:     
015:     # global parameters
016:     HD    = 'D:/doc/'
017:     MEDIA = 'F:/'
018:     PHOTO_VIEWER = 'D:/WBIN/linar160/linar.exe'
019:     PREGEXP = re.compile("\.(gif|bmp|jpe?g|tiff?)$", re.I)
020:     
021:     
022:     # functions
023:     def PhotoDirs(dir):
024:         """ It returns number of photoNN """
025:         
026:         ls_photo_dirs = [ x for x in glob.glob(os.path.join(dir, "photo[0-9][0-9]")) if os.path.isdir(x)]
027:         return ls_photo_dirs and int(ls_photo_dirs[-1][-2:]) or 0
028:         
029:     def SearchMedia(dir, reg):
030:         """It searches the removal media and returns a list of tupples (directory, [photo ...])."""
031:         assoc = []
032:         for dirpath, dirnames, files in os.walk(dir):
033:             photos = [f for f in files if reg.search(f)]
034:             if photos:
035:                 assoc.append((dirpath, photos))
036:         return assoc
037:     
038:     def MovePhotos(assoc, hd):  
039:         """ Moving photos from the removal media to the HD."""
040:         
041:         dir_of_month = os.path.join(hd, date.today().strftime("%y-%m/"))
042:         
043:         if os.path.isdir(dir_of_month):
044:             n_photo_dir = PhotoDirs(dir_of_month)
045:         else:
046:             os.mkdir(dir_of_month)
047:             n_photo_dir = 0
048:     
049:         try:
050:             total = reduce(operator.__add__, [len(photos) for d, photos in assoc])
051:         except TypeError:
052:             print "No photos in the media."
053:             sys.exit()
054:             
055:         count = 0
056:         photo_dir0=''
057:     
058:         for d, photos in assoc:
059:             n_photo_dir += 1
060:             photo_dir = os.path.join(dir_of_month, "photo%02d" % n_photo_dir)
061:             if not photo_dir0:
062:                 photo_dir0 = photo_dir
063:             os.mkdir(photo_dir)
064:             for f in photos:
065:                 f1 = os.path.join(d, f)
066:                 f2 = os.path.join(photo_dir, f)
067:                 shutil.copyfile(f1, f2)
068:                 if filecmp.cmp(f1, f2):
069:                     os.remove(f1)
070:                 else:
071:                     print "copy failed: %s => %s\n" % (f1, f2)
072:                 count += 1
073:                 print "%d/%d\r" % (count, total),
074:         return [' ', photo_dir0]
075:     
076:     # main
077:     if __name__=='__main__':
078:         os.execv(PHOTO_VIEWER, MovePhotos(SearchMedia(MEDIA, PREGEXP), HD))

[戻る]