批量归类文件

来源:互联网 发布:视频点播php源码 编辑:程序博客网 时间:2024/05/01 09:02
#将以 't[年份]开头的文件移动到各自的文件夹中import osimport shutilfilelist = os.listdir(r'D:\temp_daily19712014')base = 'D:/temp_daily19712014/'for year in range(1971,2015):    subdir = base + str(year)    os.mkdir(subdir)                #建立子文件夹#shutil.move('d:/test/1962.txt','d:/test/1962/1962.txt')for files in filelist:    for year in range(1971, 2015):        oldpos = os.path.join(base, files)           #原先路径        newpos = os.path.join(base + str(year), files)   #目标路径        if files.startswith('t' + str(year)):            #判断是否以t[year]开头                shutil.move(oldpos, newpos )              #移动
0 0
原创粉丝点击