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 数据库 (sqlite3)应用
Dec 07 Python
python利用拉链法实现字典方法示例
Mar 25 Python
python常见排序算法基础教程
Apr 13 Python
Python 爬虫之超链接 url中含有中文出错及解决办法
Aug 03 Python
python搭建服务器实现两个Android客户端间收发消息
Apr 12 Python
PyQt5每天必学之单行文本框
Apr 19 Python
Python Learning 列表的更多操作及示例代码
Aug 22 Python
Python图像的增强处理操作示例【基于ImageEnhance类】
Jan 03 Python
python笔记_将循环内容在一行输出的方法
Aug 08 Python
Python使用Opencv实现图像特征检测与匹配的方法
Oct 30 Python
Python3 实现爬取网站下所有URL方式
Jan 16 Python
Django url 路由匹配过程详解
Jan 22 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实现框架(二)
2006/10/09 PHP
PHP生成word文档的三种实现方式
2016/11/14 PHP
PHP自定义递归函数实现数组转JSON功能【支持GBK编码】
2018/07/17 PHP
PHP SESSION机制的理解与实例
2019/03/22 PHP
在laravel中使用with实现动态添加where条件
2019/10/10 PHP
php 使用expat方式解析xml文件操作示例
2019/11/26 PHP
js时间戳格式化成日期格式的多种方法
2013/11/11 Javascript
JQuery给网页更换皮肤的方法
2015/05/30 Javascript
JavaScript+Java实现HTML页面转为PDF文件保存的方法
2016/05/30 Javascript
仅9张思维导图帮你轻松学习Javascript 就这么简单
2016/06/01 Javascript
JavaScript用构造函数如何获取变量的类型名
2016/12/23 Javascript
滚动条的监听与内容随着滚动条动态加载的实现
2017/02/08 Javascript
jQuery实现简单复制json对象和json对象集合操作示例
2018/07/09 jQuery
详解Python中类的定义与使用
2017/04/11 Python
200 行python 代码实现 2048 游戏
2018/01/12 Python
python 3利用Dlib 19.7实现摄像头人脸检测特征点标定
2018/02/26 Python
详解Python做一个名片管理系统
2019/03/14 Python
python3.5安装python3-tk详解
2019/04/26 Python
Python面向对象进阶学习
2019/05/21 Python
python绘制地震散点图
2019/06/18 Python
python爬虫项目设置一个中断重连的程序的实现
2019/07/26 Python
python3中pip3安装出错,找不到SSL的解决方式
2019/12/12 Python
根据tensor的名字获取变量的值方式
2020/01/04 Python
css3的transform中scale缩放详解
2014/12/08 HTML / CSS
澳大利亚玩具剧场:Toy Playhouse
2019/03/03 全球购物
英国DVD和蓝光碟片购买网站:Zoom.co.uk(电影和电视)
2019/09/23 全球购物
大学生新闻专业个人自我评价
2013/11/12 职场文书
班组长的岗位职责
2013/12/09 职场文书
高中毕业自我评价
2014/02/08 职场文书
会走路的树教学反思
2014/02/20 职场文书
2014群众路线学习笔记
2014/11/06 职场文书
飞越疯人院观后感
2015/06/09 职场文书
在校证明模板
2015/06/17 职场文书
英语读书笔记
2015/07/02 职场文书
详解MindSpore自定义模型损失函数
2021/06/30 Python
SpringBoot实现quartz定时任务可视化管理功能
2021/08/30 Java/Android