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在Windows8下获取本机ip地址的方法
Mar 14 Python
python模块之StringIO使用示例
Apr 08 Python
python中偏函数partial用法实例分析
Jul 08 Python
使用Python导出Excel图表以及导出为图片的方法
Nov 07 Python
Python抓取框架 Scrapy的架构
Aug 12 Python
Python实现的矩阵类实例
Aug 22 Python
python测试mysql写入性能完整实例
Jan 18 Python
Python实现的堆排序算法示例
Apr 29 Python
selenium+python自动化测试之多窗口切换
Jan 23 Python
python自动化测试无法启动谷歌浏览器问题
Oct 10 Python
Python 统计位数为偶数的数字代码详解
Mar 15 Python
python 利用jieba.analyse进行 关键词提取
Dec 17 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
yii2项目实战之restful api授权验证详解
2017/05/20 PHP
?牟┛途W扣了一??效果出?? target=
2007/05/27 Javascript
Jquery下的26个实用小技巧(jQuery tips, tricks & solutions)
2010/03/01 Javascript
Javascript Cookie读写删除操作的函数
2010/03/02 Javascript
制作高质量的JQuery Plugin 插件的方法
2010/04/20 Javascript
JQuery从头学起第一讲
2010/07/04 Javascript
jQuery怎么解析Json字符串(Json格式/Json对象)
2013/08/09 Javascript
JavaScript图片轮播代码分享
2015/07/31 Javascript
JS去除空格和换行的正则表达式(推荐)
2016/06/14 Javascript
JavaScript 身份证号有效验证详解及实例代码
2016/10/20 Javascript
js编写的treeview使用方法
2016/11/11 Javascript
AngularJS 的$timeout服务示例代码
2017/09/21 Javascript
详解NODEJS基于FFMPEG视频推流测试
2017/11/17 NodeJs
Vuejs 实现简易 todoList 功能 与 组件实例代码
2018/09/10 Javascript
详解nvm管理多版本node踩坑
2019/07/26 Javascript
VUE实现自身整体组件销毁的示例代码
2020/01/13 Javascript
[00:14]PWL:老朋友Mushi拍VLOG与中国玩家问好
2020/11/04 DOTA
python uuid模块使用实例
2015/04/08 Python
pandas 对每一列数据进行标准化的方法
2018/06/09 Python
Python3 单行多行万能正则匹配方法
2019/01/07 Python
python区分不同数据类型的方法
2019/10/14 Python
什么是Python中的顺序表
2020/06/02 Python
SmartBuyGlasses台湾:名牌眼镜,名牌太阳眼镜及隐形眼镜
2017/01/04 全球购物
德国最大的设计师鞋网上商店:Budapester
2017/12/07 全球购物
Schutz鞋官方网站:Schutz Shoes
2017/12/13 全球购物
Beach Bunny Swimwear官网:设计师泳装和性感比基尼
2019/03/13 全球购物
在Ajax应用中信息是如何在浏览器和服务器之间传递的
2016/05/31 面试题
学生打架检讨书
2014/02/14 职场文书
学雷锋活动简报
2015/07/20 职场文书
2016大学生国家助学贷款承诺书
2016/03/25 职场文书
农村房屋租赁合同(范本)
2019/07/23 职场文书
redis使用不当导致应用卡死bug的过程解析
2021/07/01 Redis
MySQL系列之四 SQL语法
2021/07/02 MySQL
JavaScript实现优先级队列
2021/12/06 Javascript
一文教你快速生成MySQL数据库关系图
2022/06/28 Redis
阿里云服务器(windows)手动部署FTP站点详细教程
2022/08/05 Servers