分享

Python根据文件创建时间创建目录并移动文件到该目录

 老三的休闲书屋 2021-02-11

当一个目录下的文件过多时,查找起来十分不便,此时需要把文件分类整理一下,把文件移动到不同的文件夹下。而用文件的创建时间来分类是一个不错的方法,下面这个脚本就是把一堆图片,mp4文件根据创建时间建目录并移动进去。

#!/usr/bin/env python# -*- coding:utf-8 -*-import osimport timeimport shutil#获取当地时间,tm_isdst是否为夏令时,0表示非夏令时, _ 对计数中的实际值并不感兴趣,此时就可以使用'_'(tm_year,tm_mon,tm_mday,tm_hour,tm_min,_,_,_,_) = time.localtime()def fileTime(filePath): # 获取文件创建时间 statinfo = os.stat(filePath) # 转成时间数组, st_ctime(创建时间), 直接转成字符串 timeArr = time.localtime(statinfo.st_ctime) return timeArr # 返回时间数组def timeDir(filePath, timeArr): # 按文件年月建目录,移动文件 if not os.path.isfile(filePath): return # 检查一下是不是文件 names = filePath.split('/') root = '' for i in range(len(names)-1): root = root + names[i] + '/' temp = str(timeArr[1]) if timeArr[1] < 10: temp = '0' + temp # 月份写成 '01','02',..., '12' tempPath = os.path.join(root, temp) # 在当前目录下创建一个月份目录 if not os.path.exists(tempPath): # 在当前目录下创建月份目录 os.mkdir(tempPath) # 如果文件不是今年创建的,在月份目录下创建一个年份目录,把该文件移动进去 if timeArr[0] != tm_year: tempPath = os.path.join(tempPath, str(timeArr[0])) if not os.path.exists(tempPath): os.mkdir(tempPath) shutil.move(filePath, tempPath) # 移动文件def fileOp(root): # 移动文件操作 filends = ['jpg', 'jpeg', 'png', 'bmp', 'gif', 'webp', 'mp4'] # 需要移动的文件格式 monDirs = ['01','02','03','04','05','06','07','08','08','09','10','11','12'] # 月份目录列表 dirs = os.listdir(root) # 遍历文件目录 for dirName in dirs: if dirName=='.' or dirName=='..' or dirName in monDirs: continue # 跳过的目录 if dirName.endswith('.py'): continue # 跳过的文件 dirPath = os.path.join(root, dirName) # 拼接成完整的路径 if os.path.isfile(dirPath): # 如果是当前目录下的文件 # 获得文件创建时间的年份,月份,其余不要 year, month, _,_,_,_,_,_,_ = fileTime(dirPath) # 当前目录下的图片,mp4文件移动到对应的月份,年份目录 timeDir(dirPath, [year, month]) # 遍历当前目录下的子目录, 除去 '01','02'等当前目录下的月份目录 elif os.path.isdir(dirPath): files = os.listdir(dirPath) for file in files: filePath = os.path.join(dirPath, file) # 当前目录下的子目录的子目录略过,可能是 01 02之类的文件夹 if os.path.isdir(filePath): continue endsWith = filePath.split('.')[-1] # 文件类型 if endsWith not in filends: continue else: year,month,_,_,_,_,_,_,_ = fileTime(filePath) # 当前目录下的子目录的图片,mp4文件移动到对应的月份,年份目录 timeDir(filePath, [year, month])def reFile(rootPath): # 恢复最近10分钟移动的文件 # 遍历给定的目录 for root, dirs, files in os.walk(rootPath): for file in files: # 跳过*.py文件 if file.endswith('.py'): continue filePath = os.path.join(root, file) # 获得文件创建的年份,月份,小时,分钟 year, mon, _, hour, min, _, _, _, _ = fileTime(filePath) # 创建时间小于10分钟以内的文件 if (tm_hour * 60 + tm_min) - (hour * 60 + min) < 10: # 重新拼接路径,删掉年份,月份 tmpDirs = root.split('/') tmpPath = '' if year != tm_year: # 路径删掉年份,月份 for i in range(len(tmpDirs)-2): tmpPath = tmpPath + tmpDirs[i] + '/' else: # 路径删掉月份 for i in range(len(tmpDirs)-1): tmpPath = tmpPath + tmpDirs[i] + '/' shutil.copy(filePath, tmpPath)if __name__ == '__main__': path_root = '.' fileOp(path_root) print('需要恢复最近5分钟操作的文件吗?') prompt = input('请输入Y,y,Yes,yes,N,n,No or no: ') if (prompt.upper())[0] == 'Y': reFile(path_root) elif (prompt.upper())[0] == 'N': print('No problem')
Python根据文件创建时间创建目录并移动文件到该目录

图1 源文件截图

通过这个小程序可以熟悉系统时间,文件创建时间的用法。熟练os.listdir(),os.walk()两种方法的使用。一般情况下当前目录应该包含今年的年份数字,还有运行程序之前做个目录备份,避免失误带来的麻烦。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多