python刪除過期文件的方法
本文實(shí)例講述了python刪除過期文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
# 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
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
pymysql之cur.fetchall() 和cur.fetchone()用法詳解
這篇文章主要介紹了pymysql之cur.fetchall() 和cur.fetchone()用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05通過celery異步處理一個(gè)查詢?nèi)蝿?wù)的完整代碼
今天小編就為大家分享一篇通過celery異步處理一個(gè)查詢?nèi)蝿?wù)的完整代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11自然語言處理NLP TextRNN實(shí)現(xiàn)情感分類
這篇文章主要為大家介紹了自然語言處理NLP TextRNN實(shí)現(xiàn)情感分類示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04詳解如何用OpenCV + Python 實(shí)現(xiàn)人臉識(shí)別
這篇文章主要介紹了詳解如何用OpenCV + Python 實(shí)現(xiàn)人臉識(shí)別,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10Python實(shí)現(xiàn)深度遍歷和廣度遍歷的方法
今天小編就為大家分享一篇Python實(shí)現(xiàn)深度遍歷和廣度遍歷的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01使用Python程序抓取新浪在國(guó)內(nèi)的所有IP的教程
這篇文章主要介紹了使用Python程序抓取新浪在國(guó)內(nèi)的所有IP的教程,作為Python網(wǎng)絡(luò)編程中獲取IP的一個(gè)小實(shí)踐,需要的朋友可以參考下2015-05-05利用Python實(shí)現(xiàn)面部識(shí)別的方法詳解
人臉識(shí)別正在成為軟件開發(fā)中的一種趨勢(shì)。它有助于識(shí)別人臉并使應(yīng)用程序更加健壯。本文將使用python和face_recognition庫(kù)創(chuàng)建一個(gè)簡(jiǎn)單的人臉識(shí)別,需要的可以參考一下2022-05-05Python獲取CPU、內(nèi)存使用率以及網(wǎng)絡(luò)使用狀態(tài)代碼
這篇文章主要介紹了Python獲取CPU使用率、內(nèi)存使用率、網(wǎng)絡(luò)使用狀態(tài)的相關(guān)代碼,對(duì)此有需要的朋友一起測(cè)試下。2018-02-02