python删除过期文件的方法


Posted in Python onMay 29, 2015

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
python实现图片批量剪切示例
Mar 25 Python
Python CSV模块使用实例
Apr 09 Python
Python实现Sqlite将字段当做索引进行查询的方法
Jul 21 Python
Python字符串拼接六种方法介绍
Dec 18 Python
python线程池threadpool实现篇
Apr 27 Python
Python使用wget实现下载网络文件功能示例
May 31 Python
对python中for、if、while的区别与比较方法
Jun 25 Python
浅谈python中真正关闭socket的方法
Dec 18 Python
Python字符串通过'+'和join函数拼接新字符串的性能测试比较
Mar 05 Python
在pandas中遍历DataFrame行的实现方法
Oct 23 Python
Python远程开发环境部署与调试过程图解
Dec 09 Python
python通过opencv调用摄像头操作实例分析
Jun 07 Python
Python的Django框架中TEMPLATES项的设置教程
May 29 #Python
编写Python脚本把sqlAlchemy对象转换成dict的教程
May 29 #Python
Python fileinput模块使用实例
May 28 #Python
Python sys.argv用法实例
May 28 #Python
Python中exit、return、sys.exit()等使用实例和区别
May 28 #Python
Python中的with...as用法介绍
May 28 #Python
python关键字and和or用法实例
May 28 #Python
You might like
用PHP连mysql和oracle数据库性能比较
2006/10/09 PHP
PHP中鲜为人知的10个函数
2014/02/28 PHP
TP5.0框架实现无限极回复功能的方法分析
2019/05/04 PHP
php/JS实现的生成随机密码(验证码)功能示例
2019/06/06 PHP
JQuery 写的个性导航菜单
2009/12/24 Javascript
基于jquery的button默认enter事件(回车事件)。
2011/05/18 Javascript
javascript数组去重3种方法的性能测试与比较
2013/03/26 Javascript
浅析Nodejs npm常用命令
2016/06/14 NodeJs
AngularJS 模块详解及简单实例
2016/07/28 Javascript
详解javascript事件绑定使用方法
2016/10/20 Javascript
JS创建对象的写法示例
2016/11/04 Javascript
基于jQuery实现无缝轮播与左右点击效果
2018/05/13 jQuery
深入理解Angularjs 脏值检测
2018/10/12 Javascript
Javascript实现秒表倒计时功能
2018/11/17 Javascript
vue日历/日程提醒/html5本地缓存功能
2019/09/02 Javascript
利用js canvas实现五子棋游戏
2020/10/11 Javascript
celery4+django2定时任务的实现代码
2018/12/23 Python
Python实现的爬取百度文库功能示例
2019/02/16 Python
Python Matplotlib 基于networkx画关系网络图
2019/07/10 Python
python IDLE 背景以及字体大小的修改方法
2019/07/12 Python
如何将anaconda安装配置的mmdetection环境离线拷贝到另一台电脑
2020/10/15 Python
Html5如何唤起百度地图App的方法
2019/01/27 HTML / CSS
html5中使用hotcss.js实现手机端自适配的方法
2020/04/23 HTML / CSS
美国著名的团购网站:Woot
2016/08/02 全球购物
家得宝加拿大家装网上商店:The Home Depot加拿大
2016/08/27 全球购物
德国运动营养和健身网上商店:Myprotein.de
2018/07/18 全球购物
煤矿机修工岗位职责
2014/02/07 职场文书
文明礼貌演讲稿
2014/05/12 职场文书
兽医医药专业求职信
2014/07/27 职场文书
绿色环保家庭事迹材料
2014/08/31 职场文书
社区领导班子四风问题原因分析及整改措施
2014/09/28 职场文书
人生遥控器观后感
2015/06/11 职场文书
班级联欢会主持词
2015/07/03 职场文书
Go语言-为什么返回值为接口类型,却返回结构体
2021/04/24 Golang
Python re.sub 反向引用的实现
2021/07/07 Python
Nginx使用Lua模块实现WAF的原理解析
2021/09/04 Servers