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的Tornado框架的HTTP客户端的教程
Apr 24 Python
Python基于list的append和pop方法实现堆栈与队列功能示例
Jul 24 Python
python实现神经网络感知器算法
Dec 20 Python
简单谈谈python中的lambda表达式
Jan 19 Python
Python字典及字典基本操作方法详解
Jan 30 Python
Python实现获取nginx服务器ip及流量统计信息功能示例
May 18 Python
在pandas多重索引multiIndex中选定指定索引的行方法
Nov 16 Python
详解Python3中ceil()函数用法
Feb 19 Python
Python操作Mongodb数据库的方法小结
Sep 10 Python
简单介绍一下pyinstaller打包以及安全性的实现
Jun 02 Python
Keras SGD 随机梯度下降优化器参数设置方式
Jun 19 Python
基于Python正确读取资源文件
Sep 14 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
第十一节--重载
2006/11/16 PHP
PHP nl2br函数 将换行字符转成 <br>
2009/08/21 PHP
基于preg_match_all采集后数据处理的一点心得笔记(编码转换和正则匹配)
2014/01/31 PHP
thinkphp5使用无限极分类
2019/02/18 PHP
Thinkphp整合阿里云OSS图片上传实例代码
2019/04/28 PHP
gearman管理工具GearmanManager的安装与php使用方法示例
2020/02/27 PHP
getElementById在任意一款浏览器中都可以用吗的疑问回复
2007/05/13 Javascript
(JS实现)MapBar中坐标的加密和解密的脚本
2007/05/16 Javascript
js+css简单实现网页换肤效果
2015/12/29 Javascript
把多个JavaScript函数绑定到onload事件处理函数上的方法
2016/09/04 Javascript
JSON字符串和JSON对象相互转化实例详解
2017/01/05 Javascript
ionic2 tabs 图标自定义实例
2017/03/08 Javascript
Angular2下使用pdf插件的方法详解
2017/04/29 Javascript
使用Require.js封装原生js轮播图的实现代码
2017/06/15 Javascript
webpack下实现动态引入文件方法
2018/02/22 Javascript
vue+element-ui+ajax实现一个表格的实例
2018/03/09 Javascript
如何使用vuex实现兄弟组件通信
2018/11/02 Javascript
element-ui table行点击获取行索引(index)并利用索引更换行顺序
2020/02/27 Javascript
vue项目接口域名动态获取操作
2020/08/13 Javascript
Vue实现鼠标经过文字显示悬浮框效果的示例代码
2020/10/14 Javascript
[01:01:36]Optic vs paiN 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
30秒轻松实现TensorFlow物体检测
2018/03/14 Python
python 获取list特定元素下标的实例讲解
2018/04/09 Python
Pandas透视表(pivot_table)详解
2019/07/22 Python
python3通过udp实现组播数据的发送和接收操作
2020/05/05 Python
浅析Python面向对象编程
2020/07/10 Python
哪种Python框架适合你?简单介绍几种主流Python框架
2020/08/04 Python
Python操作word文档插入图片和表格的实例演示
2020/10/25 Python
如何编写python的daemon程序
2021/01/07 Python
CSS3使用border-radius属性制作圆角
2014/12/22 HTML / CSS
俄罗斯三星品牌商店:GalaxyStore
2020/11/04 全球购物
西安众合通用.net笔试题
2013/03/18 面试题
大学校园活动策划书
2014/02/04 职场文书
升学宴答谢词
2015/01/05 职场文书
大学生创业,为什么都会选择快餐饮?
2019/08/08 职场文书
Python机器学习之基于Pytorch实现猫狗分类
2021/06/08 Python