• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python删除过期文件的方法

python删除过期文件的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python打开文件方法,python按行读取文件,python读取txt文件,python运行py文件,python 删除文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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

</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python实现下载文件的三种方法
  • Python实现大文件排序的方法
  • python检查指定文件是否存在的方法
  • python删除过期文件的方法
  • python删除过期文件的方法
  • Python格式化css文件的方法
  • 简单文件操作python 修改文件指定行的方法

相关文章

  • Python读取Excel的方法实例分析
  • python同义词替换的实现(jieba分词)
  • python编写爬虫小程序
  • Python实现的几个常用排序算法实例
  • Python的多态性实例分析
  • Python实现批量将word转html并将html内容发布至网站的方法
  • Python selenium 三种等待方式解读
  • python基础教程之popen函数操作其它程序的输入和输出示例
  • 详解Python使用simplejson模块解析JSON的方法
  • python类装饰器用法实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python的Django框架可适配的各种数据库介绍
    • python Django连接MySQL数据库做增删改查
    • Python常用的爬虫技巧总结
    • python制作websocket服务器实例分享
    • 零基础写python爬虫之抓取百度贴吧并存储到本地txt文件改进版
    • python中遍历文件的3个方法
    • python实现自动重启本程序的方法
    • 使用Python获取Linux系统的各种信息
    • 浅谈五大Python Web框架
    • 跟老齐学Python之使用Python操作数据库(1)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有