日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Python根據(jù)文件創(chuàng)建時(shí)間創(chuàng)建目錄并移動(dòng)文件到該目錄

 老三的休閑書屋 2021-02-11

當(dāng)一個(gè)目錄下的文件過多時(shí),查找起來十分不便,此時(shí)需要把文件分類整理一下,把文件移動(dòng)到不同的文件夾下。而用文件的創(chuàng)建時(shí)間來分類是一個(gè)不錯(cuò)的方法,下面這個(gè)腳本就是把一堆圖片,mp4文件根據(jù)創(chuàng)建時(shí)間建目錄并移動(dòng)進(jìn)去。

#!/usr/bin/env python# -*- coding:utf-8 -*-import osimport timeimport shutil#獲取當(dāng)?shù)貢r(shí)間,tm_isdst是否為夏令時(shí),0表示非夏令時(shí), _ 對計(jì)數(shù)中的實(shí)際值并不感興趣,此時(shí)就可以使用'_'(tm_year,tm_mon,tm_mday,tm_hour,tm_min,_,_,_,_) = time.localtime()def fileTime(filePath): # 獲取文件創(chuàng)建時(shí)間 statinfo = os.stat(filePath) # 轉(zhuǎn)成時(shí)間數(shù)組, st_ctime(創(chuàng)建時(shí)間), 直接轉(zhuǎn)成字符串 timeArr = time.localtime(statinfo.st_ctime) return timeArr # 返回時(shí)間數(shù)組def timeDir(filePath, timeArr): # 按文件年月建目錄,移動(dòng)文件 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) # 在當(dāng)前目錄下創(chuàng)建一個(gè)月份目錄 if not os.path.exists(tempPath): # 在當(dāng)前目錄下創(chuàng)建月份目錄 os.mkdir(tempPath) # 如果文件不是今年創(chuàng)建的,在月份目錄下創(chuàng)建一個(gè)年份目錄,把該文件移動(dòng)進(jìn)去 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) # 移動(dòng)文件def fileOp(root): # 移動(dòng)文件操作 filends = ['jpg', 'jpeg', 'png', 'bmp', 'gif', 'webp', 'mp4'] # 需要移動(dòng)的文件格式 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): # 如果是當(dāng)前目錄下的文件 # 獲得文件創(chuàng)建時(shí)間的年份,月份,其余不要 year, month, _,_,_,_,_,_,_ = fileTime(dirPath) # 當(dāng)前目錄下的圖片,mp4文件移動(dòng)到對應(yīng)的月份,年份目錄 timeDir(dirPath, [year, month]) # 遍歷當(dāng)前目錄下的子目錄, 除去 '01','02'等當(dāng)前目錄下的月份目錄 elif os.path.isdir(dirPath): files = os.listdir(dirPath) for file in files: filePath = os.path.join(dirPath, file) # 當(dāng)前目錄下的子目錄的子目錄略過,可能是 01 02之類的文件夾 if os.path.isdir(filePath): continue endsWith = filePath.split('.')[-1] # 文件類型 if endsWith not in filends: continue else: year,month,_,_,_,_,_,_,_ = fileTime(filePath) # 當(dāng)前目錄下的子目錄的圖片,mp4文件移動(dòng)到對應(yīng)的月份,年份目錄 timeDir(filePath, [year, month])def reFile(rootPath): # 恢復(fù)最近10分鐘移動(dòng)的文件 # 遍歷給定的目錄 for root, dirs, files in os.walk(rootPath): for file in files: # 跳過*.py文件 if file.endswith('.py'): continue filePath = os.path.join(root, file) # 獲得文件創(chuàng)建的年份,月份,小時(shí),分鐘 year, mon, _, hour, min, _, _, _, _ = fileTime(filePath) # 創(chuàng)建時(shí)間小于10分鐘以內(nèi)的文件 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('需要恢復(fù)最近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根據(jù)文件創(chuàng)建時(shí)間創(chuàng)建目錄并移動(dòng)文件到該目錄

圖1 源文件截圖

通過這個(gè)小程序可以熟悉系統(tǒng)時(shí)間,文件創(chuàng)建時(shí)間的用法。熟練os.listdir(),os.walk()兩種方法的使用。一般情況下當(dāng)前目錄應(yīng)該包含今年的年份數(shù)字,還有運(yùn)行程序之前做個(gè)目錄備份,避免失誤帶來的麻煩。

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多