python实现的文件夹清理程序分享
作者:junjie 发布时间:2021-07-20 07:58:57
使用:
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])
猜你喜欢
- Request.ServerVariables里Server_Name与Http_Host之间有什么区别呢?如果不仔细看,您应该看不出它们之
- 使用thinkphp官方的WeChat包,使用不同模式可以成功,但是安全模式就是不行,现将分析解决结果做下记录。分析问题: &nb
- 八种获取当前日期的js代码,第一步:把如下代码加入<head>区域中:<SCRIPT language=java
- Web,全称为 World Wide Web,是 Internet 上最重要和最为人们所熟知的应用之一。Web 是指 Internet 上所
- 随着SaaS服务的流行,越来越多的人选择在各个平台上编写文档,制作表格并进行分享。同时,随着Markdown语法的破圈,很多平台开始集成支持
- 图像(包括动图)是传递信息的一种高效方式,往往能增强表象、记忆与思维等方面的反应强度。所谓一图胜千言,说的就是这个道理。今天为大家整理了十张
- 代码如下:--创建测试表 DECLARE @Users TABLE ( ID INT IDENTITY(1,1), UserIn
- 本文实例讲述了PHP依赖注入原理与用法。分享给大家供大家参考,具体如下:引言依然是来自到喜啦的一道面试题,你知道什么是依赖注入吗?依赖注入(
- 在日常的工作中,保护数据免受未授权用户的侵犯是系统管理员特别关心的问题。如果你目前用的是MySQL,就可以使用一些方便的功能来保护系统,来大
- <1>IsArray 函数 返回 Boolean 值指明某变量是否为数组。 语法 IsArray(var
- 抽象方法和抽象类 在OOP 语言中,一个类可以有一个或多个子类,而每个类都有至少一个公有方法做为 外部代码访问其的接口。而抽象方法就是为了方
- thinkphp查询mssql数据库出现乱码的原因是ThinkPHP默认为UTF-8,而msmsql数据库是简体中文版,存储的是GB2312
- 今天研究了些取access数据库随机记录问题,这是这我自己搜集整理的方法。大家有没有高见,可以告诉我,或者我总结的东东本身有误,也可以帮我修
- 题目给定一个字符串,逐个翻转字符串中的每个单词。示例 1:输入: "the sky is blue"输出: "
- 例如:我们在百度中搜索 词典网,则网址后面的参数就是http://www.baidu.com/s?cl=3&wd=%B4%CA%B5
- 本文实例讲述了php实现通过cookie换肤的方法。分享给大家供大家参考。具体如下:saveStyleSheet.php页面如下:<?
- 导语:目前点评“2008年10佳改版网站”也许为时尚早,但2008年毕竟已经过去了9个多月,周四又同时有Twitter和FriendFeed
- 细节汇总函数的形参列表可以是多个,返回值列表也可以是多个形参列表和返回值列表的数据类型,可以是值类型、也可以是引用类型函数的命名遵循标识符命
- 1、PHP 中如何正确统计中文字数?这个是困扰我很久的问题,PHP 中有很多函数可以计算字符串的长度,比如下面的例子,分别使用了
- 首先,与其他语言不同,JS的效率很大程度是取决于JS engine的效率。除了引擎实现的优劣外,引擎自己也会为一些特殊的代码模式采取一些优化