目的: 处理windows系统文件迁移,文件格式包含特殊字符(空格,括号,全角等)
语言: python
模块: shutil
代码:
#coding:utf-8 import os,sys import time import shutil def move_file(basedir,bank_num): bankdir=os.path.join(basedir,bank_num) for file in os.listdir(bankdir): filepath=os.path.join(bankdir,file) if os.path.isfile(filepath): curDatetime=time.strftime("%Y",time.localtime(os.path.getmtime(filepath))) if curDatetime != "2015": dstfilepath=os.path.join(bankdir,curDatetime) dstfilepath+="/" #这个要注意,如果不加这个,那所有文件将move到同一文件而不是同一目录 shutil.move(filepath,dstfilepath) print filepath if __name__ == "__main__": if len(sys.argv) != 2: print "Usage: %s bank_code" % sys.argv[0] os._exit(1) bank_num=sys.argv[1] basedir="/cygdrive/e/uploadfile" move_file(basedir,bank_num)