python删除过期文件的方法

   本文实例讲述了python删除过期文件的方法。分享给大家供大家参考。具体实现方法如下:

  ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 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:VacationPoland2003' # 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程序设计有所帮助。

时间: 2024-05-29 23:58:52

python删除过期文件的方法的相关文章

Linux下两种删除过期文件的方法详述

一.概述 在实际的C软件开发项目中,不同的软件会在不同的目录中生成文件,由于磁盘的存储空间有限,开发人员不得不考虑对目录下的过期文件进行删除.一般说来,有两种删除过期文件的方法,一种是在C程序中实现,一种是利用crontab实现.本文对这两种方法的具体实现进行详细的介绍. 为了便于说明,本文中的过期文件的后缀为.c,存放在/home/zhou/zhouzx/Test目录下,过期时间为1天. 二.在C程序中实现过期文件删除 在该方法中,我们要考虑的主要问题为: (1)要删除过期多久的文件?文件存放

Linux下定时删除过期文件的方法

在管理Linux下的文件时,我们通常会有定时删除过期文件的这个需求.比如定时删除超过指定时间的日志文件,不然时长越久,你的日志文件占用的空间就越大,那离爆磁盘不远了,还有也解决网站方便的需求,比如我们建立的淘宝客api的程序,为了提高商品页的访问速度,我们需要进行关键数据的缓存,也需要定时删除过期的缓存文件,如果是使用PHP程序扫描删除,那将得不偿失.那么如何在Linux下定时删除过期文件?这里小编简单介绍下,方便需要的朋友. 在linux下删除过期文件,很简单,用如下命令: 复制代码代码如下:

Python读写ini文件的方法

  本文实例讲述了Python读写ini文件的方法.分享给大家供大家参考.具体如下: 比如有一个文件update.ini,里面有这些内容: ? 1 2 3 4 5 6 7 8 [ZIP] EngineVersion=0 DATVersion=5127 FileName=dat-5127.zip FilePath=/pub/antivirus/datfiles/4.x/ FileSize=13481555 Checksum=6037,021E MD5=aaeb519d3f276b810d46642

Python修改MP3文件的方法

  本文实例讲述了Python修改MP3文件的方法.分享给大家供大家参考.具体如下: 用这个程序修改后的MP3比原来要小一些了,因为一张图片被删除了,起到了给MP3"瘦身"的作用.在一些mp3中,每个都有一张400多K的图片,10几个MP3,就相当一个普通MP3文件的大小了. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

python引用DLL文件的方法

  本文实例讲述了python引用DLL文件的方法.分享给大家供大家参考.具体分析如下: 在python中调用dll文件中的接口比较简单,如我们有一个test.dll文件,内部定义如下: ? 1 2 3 4 5 6 7 extern "C" { int __stdcall test( void* p, int len) { return len; } } 在python中我们可以用以下两种方式载入 1. ? 1 2 import ctypes dll = ctypes.windll.L

python写入xml文件的方法

  这篇文章主要介绍了python写入xml文件的方法,涉及Python操作XML模块实现针对XML文件操作的相关技巧,需要的朋友可以参考下 本范例通过xml模块对xml文件进行写入操作 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 from xml.dom.minidom import Document doc = Document() people = doc.createElement("people") doc.appendChild(people) a

Python格式化css文件的方法_python

本文实例讲述了Python格式化css文件的方法.分享给大家供大家参考.具体实现方法如下: import string, sys import re, StringIO TAB=4 def format(ss): f = open (ss, "r") data = f.read() f.close() dlen = len(data) i = 0 buf = StringIO.StringIO() start = 0 while i < dlen: if data[i] == '

Python生成pdf文件的方法_python

本文实例演示了Python生成pdf文件的方法,是比较实用的功能,主要包含2个文件.具体实现方法如下: pdf.py文件如下: #!/usr/bin/python from reportlab.pdfgen import canvas def hello(): c = canvas.Canvas("helloworld.pdf") c.drawString(100,100,"Hello,World") c.showPage() c.save() hello() di

Linux下的删除过期文件操作

        在实际的软件开发项目中,经常会有实现删除过期文件的需求.即要求程序能够自动删除多长时间之前生成的文件.本文提供了一个示例代码,为相关的开发工作提供了参考.         程序如下(本程序用makefile进行编译): /*********************************************************************** 版权所有 (C)2014, Zhou Zhaoxiong.* * 文件名称: TestFileDelete.c* 内容