python 删除指定时间间隔之前的文件
来源:互联网 发布:mysql事物隔离级别 编辑:程序博客网 时间:2024/06/07 03:41
遍历指定文件夹下的文件,根据文件后缀名,获取指定类型的文件列表;根据文件列表里的文件路径,逐个获取文件属性里的“修改时间”,如果“修改时间”与“系统当前时间”差值大于某个值,则删除该文件。
#!/usr/bin/env python# -*- coding: utf-8 -*-"""Document: Remove Synctoycmd sync expired .tmp files"""import osimport timeimport datetimedef diff(): '''time diff''' starttime = datetime.datetime.now() time.sleep(10) endtime = datetime.datetime.now() print "time diff: %d" % ((endtime-starttime).seconds)def fileremove(filename, timedifference): '''remove file''' date = datetime.datetime.fromtimestamp(os.path.getmtime(filename)) print date now = datetime.datetime.now() print now print 'seconds difference: %d' % ((now - date).seconds) if (now - date).seconds > timedifference: if os.path.exists(filename): os.remove(filename) print 'remove file: %s' % filename else: print 'no such file: %s' % filenameFILE_DIR = 'D:/'if __name__ == '__main__': print 'Script is running...' #diff() while True: ITEMS = os.listdir(FILE_DIR) NEWLIST = [] for names in ITEMS: if names.endswith(".txt"): NEWLIST.append(FILE_DIR + names) #print NEWLIST for names in NEWLIST: print 'current file: %s' % (names) fileremove(names, 10) time.sleep(10) print "never arrive..."
阅读全文
0 0
- python 删除指定时间间隔之前的文件
- shell删除指定时间之前的文件
- 【Shell】删除指定时间之前的文件
- 删除指定时间之前的文件
- find删除指定时间之前的文件
- 【日期时间】批处理删除指定天数日期之前的文件
- linux下删除指定时间之前的文件
- linxu中删除指定时间之前的文件shell脚本
- Shell定时删除指定时间之前的文件
- bat删除指定时间之前文件
- linux 删除指定日期之前的文件
- linux 删除指定日期之前的文件
- linux 删除指定日期之前的文件
- 删除某段时间之前的文件
- 【日期时间】批处理删除指定天数日期之前的文件夹
- 批处理删除指定天数日期之前的文件
- 批处理删除指定天数日期之前的文件
- BAT 实现删除指定天数之前的文件
- 国外牛人总结的机器学习领域的框架、库以及软件
- Xcode8以及iOS10问题记录
- ajax开发:ajax详解
- CSS设置页面滚动条的样式
- 分治法,动态规划及贪心算法区别
- python 删除指定时间间隔之前的文件
- PCB电路板周围那一圈过孔或金属包边是做啥用的
- poj 1284 求原根
- static 与 extern
- 指针数组和数组指针的区别
- 图解 Android 事件分发机制
- 2017年PHP培训机构排名
- 互联网架构(1):并发编程--多线程基础
- 修改编辑文件夹名称, put数据出现的问题