python删除指定天数前的文件_python 删除指定时间间隔之前的文件实例
遍歷指定文件夾下的文件,根據(jù)文件后綴名,獲取指定類型的文件列表;根據(jù)文件列表里的文件路徑,逐個(gè)獲取文件屬性里的“修改時(shí)間”,如果“修改時(shí)間”與“系統(tǒng)當(dāng)前時(shí)間”差值大于某個(gè)值,則刪除該文件。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Document: Remove Synctoycmd sync expired .tmp files"""
import os
import time
import datetime
def 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' % filename
FILE_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..."
以上這篇python 刪除指定時(shí)間間隔之前的文件實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持萬仟網(wǎng)。
希望與廣大網(wǎng)友互動(dòng)??
點(diǎn)此進(jìn)行留言吧!
總結(jié)
以上是生活随笔為你收集整理的python删除指定天数前的文件_python 删除指定时间间隔之前的文件实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 序列标注(Sequence Taggin
- 下一篇: Python实现图像信息隐藏