2013年8月17日星期六

Python根据下载的图片日期创建文件夹并自动整理

__author__ = 'jimshen'
#coding=utf-8

import os, os.path, sys, time, string, shutil, random

mydir = unicode('e:\\download', 'utf8')
picstore = unicode('e:\\图片', 'utf8')
files = os.listdir(mydir)
for f in files:
    filename = mydir + os.sep + f
    if os.path.isdir(filename):
        continue
    ext = os.path.splitext(filename)[1].lower()
    if(ext=='.gif' or ext=='.jpg' or ext=='.jpeg' or ext=='.png'):
        mtime = time.strftime("%Y-%m-%d",
                              time.localtime(os.path.getmtime(filename)))
        newfilename = time.strftime("%Y%m%d%H%M%S",
                                    time.localtime(os.path.getmtime(filename))) \
                      + str(random.randint(100000, 999999)) + os.path.splitext(filename)[1]
        foldername = picstore + os.sep + mtime
        dstfilename = foldername + os.sep + newfilename
        if not os.path.exists(foldername):
            os.mkdir(foldername)
        shutil.move(filename, dstfilename)
        print f + " moved to " + dstfilename

1 条评论: