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新手实现2048小游戏
Mar 31 Python
python常见的格式化输出小结
Dec 15 Python
Python 中Pickle库的使用详解
Feb 24 Python
详解Django项目中模板标签及模板的继承与引用(网站中快速布置广告)
Mar 27 Python
Python3 实现文件批量重命名示例代码
Jun 03 Python
Python 实现输入任意多个数,并计算其平均值的例子
Jul 16 Python
基于Django静态资源部署404的解决方法
Jul 28 Python
python 并发编程 阻塞IO模型原理解析
Aug 20 Python
下载官网python并安装的步骤详解
Oct 12 Python
python3实现elasticsearch批量更新数据
Dec 03 Python
python利用opencv保存、播放视频
Nov 02 Python
Python软件包安装的三种常见方法
Jul 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
Linux下PHP安装mcrypt扩展模块笔记
2014/09/10 PHP
php版微信开发Token验证失败或请求URL超时问题的解决方法
2016/09/23 PHP
我见过最全的个人js加解密功能页面
2007/12/12 Javascript
JavaScript constructor和instanceof,JSOO中的一对欢喜冤家
2009/05/25 Javascript
2012年开发人员的16款新鲜的jquery插件体验分享
2012/12/28 Javascript
js动态创建表格,删除行列的小例子
2013/07/20 Javascript
JQuery伸缩导航练习示例
2013/11/13 Javascript
禁止空格提交表单的js代码
2013/11/17 Javascript
javascript鼠标滑动评分控件完整实例
2015/05/13 Javascript
举例说明如何为JavaScript的方法参数设置默认值
2015/11/17 Javascript
js立即执行函数: (function ( ){})( ) 与 (function ( ){}( )) 有什么区别?
2015/11/18 Javascript
javascript每日必学之封装
2016/02/23 Javascript
NodeJS实现客户端js加密
2017/01/09 NodeJs
超出JavaScript安全整数限制的数字计算BigInt详解
2018/06/24 Javascript
详解Vue中watch对象内属性的方法
2019/02/01 Javascript
浅谈JS中this在各个场景下的指向
2019/08/14 Javascript
[42:50]NB vs VP 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
Python 用Redis简单实现分布式爬虫的方法
2017/11/23 Python
浅谈numpy数组的几种排序方式
2017/12/15 Python
使用python和Django完成博客数据库的迁移方法
2018/01/05 Python
pygame游戏之旅 如何制作游戏障碍
2018/11/20 Python
python正则表达式匹配[]中间为任意字符的实例
2018/12/25 Python
python 实现将小图片放到另一个较大的白色或黑色背景图片中
2019/12/12 Python
python GUI库图形界面开发之PyQt5窗口类QMainWindow详细使用方法
2020/02/26 Python
解决pycharm安装第三方库失败的问题
2020/05/09 Python
如何把python项目部署到linux服务器
2020/08/26 Python
中职应届生会计求职信
2013/10/23 职场文书
社区道德讲堂实施方案
2014/03/21 职场文书
个人自我鉴定总结
2014/03/25 职场文书
村安全生产责任书
2014/08/25 职场文书
先进班组材料范文
2014/12/25 职场文书
收入证明范本
2015/06/12 职场文书
总经理2015中秋节致辞
2015/07/29 职场文书
幼儿园小朋友毕业感言
2015/07/30 职场文书
python代码实现扫码关注公众号登录的实战
2021/11/01 Python
《王者天下》第4季首话新剧照 4月9日正式开播
2022/04/07 日漫