国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python刪除過期文件的方法

2020-01-04 18:09:37
字體:
供稿:網(wǎng)友

這篇文章主要介紹了python刪除過期文件的方法,涉及Python日期與文件的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了python刪除過期文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

 

 
  1. # remove all jpeg image files of an expired modification date = mtime 
  2. # you could also use creation date (ctime) or last access date (atime) 
  3. # os.stat(filename) returns (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) 
  4. # tested with Python24 vegaseat 6/7/2005 
  5. import os, glob, time 
  6. root = 'D://Vacation//Poland2003//' # one specific folder 
  7. #root = 'D://Vacation//*' # or all the subfolders too 
  8. # expiration date in the format YYYY-MM-DD 
  9. xDate = '2003-12-31' 
  10. print '-'*50 
  11. for folder in glob.glob(root): 
  12. print folder 
  13. # here .jpg image files, but could be .txt files or whatever 
  14. for image in glob.glob(folder + '/*.jpg'): 
  15. # retrieves the stats for the current jpeg image file 
  16. # the tuple element at index 8 is the last-modified-date 
  17. stats = os.stat(image) 
  18. # put the two dates into matching format  
  19. lastmodDate = time.localtime(stats[8]) 
  20. expDate = time.strptime(xDate, '%Y-%m-%d'
  21. print image, time.strftime("%m/%d/%y", lastmodDate) 
  22. # check if image-last-modified-date is outdated 
  23. if expDate > lastmodDate: 
  24. try
  25. print 'Removing', image, time.strftime("(older than %m/%d/%y)", expDate) 
  26. #os.remove(image) # commented out for testing 
  27. except OSError: 
  28. print 'Could not remove', image 

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 潍坊市| 从江县| 若羌县| 射阳县| 宝应县| 清新县| 梅州市| 浮梁县| 赫章县| 东丽区| 孙吴县| 赤水市| 高邑县| 呼和浩特市| 邢台市| 黄龙县| 左贡县| 万盛区| 宁化县| 北票市| 伊吾县| 新龙县| 安平县| 莱西市| 西藏| 阳谷县| 蒲城县| 广平县| 类乌齐县| 宜兰县| 汾阳市| 福州市| 宁津县| 登封市| 房产| 道孚县| 疏附县| 绵竹市| 泽库县| 永嘉县| 农安县|