首页 后端开发 Python教程 python删除过期文件的方法

python删除过期文件的方法

Jun 06, 2016 am 11:17 AM
python 删除

本文实例讲述了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程序设计有所帮助。

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

两个点博物馆:邦格荒地地点指南
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

两个点博物馆:邦格荒地地点指南
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

抖音聊天记录怎么彻底消除干净 抖音聊天记录怎么彻底消除干净 May 07, 2024 am 11:14 AM

抖音聊天记录怎么彻底消除干净

怎么下载deepseek 小米 怎么下载deepseek 小米 Feb 19, 2025 pm 05:27 PM

怎么下载deepseek 小米

模板化的优点和缺点有哪些? 模板化的优点和缺点有哪些? May 08, 2024 pm 03:51 PM

模板化的优点和缺点有哪些?

Google AI 为开发者发布 Gemini 1.5 Pro 和 Gemma 2 Google AI 为开发者发布 Gemini 1.5 Pro 和 Gemma 2 Jul 01, 2024 am 07:22 AM

Google AI 为开发者发布 Gemini 1.5 Pro 和 Gemma 2

仅用250美元,Hugging Face技术主管手把手教你微调Llama 3 仅用250美元,Hugging Face技术主管手把手教你微调Llama 3 May 06, 2024 pm 03:52 PM

仅用250美元,Hugging Face技术主管手把手教你微调Llama 3

分享几个.NET开源的AI和LLM相关项目框架 分享几个.NET开源的AI和LLM相关项目框架 May 06, 2024 pm 04:43 PM

分享几个.NET开源的AI和LLM相关项目框架

golang 函数调试与分析的完整指南 golang 函数调试与分析的完整指南 May 06, 2024 pm 02:00 PM

golang 函数调试与分析的完整指南

deepseek怎么问他 deepseek怎么问他 Feb 19, 2025 pm 04:42 PM

deepseek怎么问他

See all articles