码迷,mamicode.com
首页 > 编程语言 > 详细

python脚本删除n天之前的文件

时间:2017-03-10 22:38:02      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:python删除n天之前的文件

管理Linux经常用到python脚本,然后写了脚本后,经常为了生成的文件占用磁盘空间而犯愁,这些写个函数以方便以后使用:

def rmdaybefore(pfile, days):
    """
    Delete pfile diectory days before files below
    :param pfile: local path
    :param days: before days
    :return: a list os.listdir pfile
    """
    d = 0
    try:
        d = int(days)
    except ValueError,e:
        print "You input the parameters of the days cannot be converted to int."
        sys.exit(1)
    BEDAYS = time.time() - (24 * 60 * 60 * d)
    if os.path.isdir(pfile):
        for f in os.listdir(pfile):
            fname = pfile + os.sep + f
            if os.path.isfile(fname):
                fmtime = os.path.getmtime(fname)
                if fmtime <= BEDAYS:
                    os.remove(fname)
                    return os.listdir(pfile)
    else:
        "You input the parameters of the pfile is not a directory."
        sys.exit(1)
if __name__ == ‘__main__‘:
    pfile = r"C:\\Users\\XXX\\Desktop\\html\\"
    for f in rmdaybefore(pfile,0.2):
        print f


本文出自 “-=湖边竹=-” 博客,请务必保留此出处http://bronte.blog.51cto.com/2418552/1905282

python脚本删除n天之前的文件

标签:python删除n天之前的文件

原文地址:http://bronte.blog.51cto.com/2418552/1905282

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!