使用:
复制代码 代码如下:
foldercleanup.py -d 10 -k c:\test\keepfile.txt c:\test
表示对c:\test目录只保留最近10天的子文件夹和keepfile.txt中指定的子文件夹。
代码:
复制代码 代码如下:
import os
import os.path
import datetime
def getOption():
from optparse import OptionParser
des = "clean up the folder with some options"
prog = "foldercleanup"
ver = "%prog 0.0.1"
usage = "%prog [options] foldername"
p = OptionParser(description=des, prog=prog, version=ver, usage=usage,add_help_option=True)
p.add_option('-d','--days',action='store',type='string',dest='days',help="keep the subfolders which are created in recent %days% days")
p.add_option('-k','--keepfile',action='store',type='string',dest='keepfile',help="keep the subfolders which are recorded in text file %keepfile% ")
options, arguments = p.parse_args()
if len(arguments) != 1:
print("error: must input one directory as only one parameter ")
return
return options.days, options.keepfile, arguments[0]
def preCheckDir(dir):
if(not os.path.exists(dir)):
print("error: the directory your input is not existed")
return
if(not os.path.isdir(dir)):
print ("error: the parameter your input is not a directory")
return
return os.path.abspath(dir)
def isKeepByDay(dir, day):
indays = False
if( day is not None) :
t = os.path.getctime(dir)
today = datetime.date.today()
createdate = datetime.date.fromtimestamp(t)
indate = today - datetime.timedelta(days = int(day))
print (createdate)
if(createdate >= indate):
indays = True
print (indays)
return indays
def isKeepByKeepfile(dir, keepfile):
needkeep = False
print (dir)
if (keepfile is not None):
try :
kf = open(keepfile,"r")
for f in kf.readlines():
print (f)
if (dir.upper().endswith("\\" + f.strip().upper())):
needkeep = True
kf.close()
except:
print ("error: keep file cannot be opened")
print(needkeep)
return needkeep
def removeSubFolders(dir, day, keepfile):
subdirs = os.listdir(dir)
for subdir in subdirs:
subdir = os.path.join(dir,subdir)
if ( not os.path.isdir(subdir)):
continue
print("----------------------")
if( (not isKeepByDay(subdir, day))and (not isKeepByKeepfile(subdir, keepfile))):
print("remove subfolder: " + subdir)
import shutil
shutil.rmtree(subdir,True)
def FolderCleanUp():
(day, keepfile, dir) = getOption()
dir = preCheckDir(dir)
if dir is None:
return
removeSubFolders(dir,day,keepfile)
if __name__=='__main__':
FolderCleanUp()
对目录下保留最后的zip文件:
复制代码 代码如下:
def KeepLastNumZips(num)
def extractTime(f):
return os.path.getctime(f)
zipfiles = [os.path.join(zipdir, f)
for f in os.listdir(zipdir)
if os.path.splitext(f)[1] == ".zip"]
if len(zipfiles) > num:
zipfiles.sort(key=extractTime, reverse=True)
for i in range(num, len(zipfiles)):
os.remove(zipfiles[i])
python,文件夹清理
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]