2012년 1월 9일 월요일

python source dst file move date

import os
import glob
import time
import sys
     
if len(sys.argv)>1:
    dir = sys.argv[1]
else:
   
#    dir = os.getcwd()
    dir = "/home/jonglae/Pictures/2011-01-24"

   
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
    dst = "/homg/jonglae/temp/"

    (dd, fn) = os.path.split(f)
    datefolder = dst + os.path.join(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.")

댓글 없음: