python删除过期文件的方法
Jun 06, 2016 am 11:17 AM
python
删除
本文实例讲述了python删除过期文件的方法。分享给大家供大家参考。具体实现方法如下:
# remove all jpeg image files of an expired modification date = mtime # you could also use creation date (ctime) or last access date (atime) # os.stat(filename) returns (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) # tested with Python24 vegaseat 6/7/2005 import os, glob, time root = 'D:\\Vacation\\Poland2003\\' # one specific folder #root = 'D:\\Vacation\\*' # or all the subfolders too # expiration date in the format YYYY-MM-DD xDate = '2003-12-31' print '-'*50 for folder in glob.glob(root): print folder # here .jpg image files, but could be .txt files or whatever for image in glob.glob(folder + '/*.jpg'): # retrieves the stats for the current jpeg image file # the tuple element at index 8 is the last-modified-date stats = os.stat(image) # put the two dates into matching format lastmodDate = time.localtime(stats[8]) expDate = time.strptime(xDate, '%Y-%m-%d') print image, time.strftime("%m/%d/%y", lastmodDate) # check if image-last-modified-date is outdated if expDate > lastmodDate: try: print 'Removing', image, time.strftime("(older than %m/%d/%y)", expDate) #os.remove(image) # commented out for testing except OSError: print 'Could not remove', image
登录后复制
希望本文所述对大家的Python程序设计有所帮助。
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章
两个点博物馆:邦格荒地地点指南
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章
两个点博物馆:邦格荒地地点指南
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章标签

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Google AI 为开发者发布 Gemini 1.5 Pro 和 Gemma 2

仅用250美元,Hugging Face技术主管手把手教你微调Llama 3
