2012년 1월 17일 화요일

화일 생성 날짜에 맞춰 폴더생성


import os
import glob
import time
import sys
         
if len(sys.argv)>1:
    dir = sys.argv[1]
else:
   
#    dir = os.getcwd()
# working dir & dst directory setting


    dir = "/home/jonglae/Pictures/2011-01-24"
    dst = "/home/jonglae/temp/"
   
print("working directory:", dir)
postfix = ""
#postfix = input("any postfix? ")


filter = os.path.join(dir, "*")
files = glob.glob(filter)
#files = os.listdir(dir)


for f in files:
    # if f is dir, skip
    if os.path.isdir(f)==True:
        continue
   
    # get modified date
    stats = os.stat(f)
    mt = time.gmtime(stats.st_mtime)
    date = time.strftime("%Y-%m-%d", mt)


    if len(postfix)>0:
        date = date + " " + postfix


    # mkdir if need
   
    (dd, fn) = os.path.split(f)
    datefolder2 =  os.path.join(dd, date)
    datefolder =  os.path.join(dst, date)
    try: os.stat(datefolder)

    except: os.mkdir(datefolder)
    #move file

    f2 = os.path.join(dst, date, fn)
    print(f,"---->",f2)
try: os.rename(f, f2)
except:
        print("    : couldn\'t move.")


#input("done. enter plz.")

댓글 없음: