2012-12-07 85 views
2

我怎麼能簡潔地表達「得到比X天的所有文件夾」得到了比X天的所有文件夾(os.walk),刪除

我有一個方法getOldDirs(dirPath,olderThanDays),它必須遍歷給定的根文件夾並返回比7天更早的文件夾列表。

我從另一個函數cleanOldFolders()調用上述函數。 cleanOldFolders()會刪除類似「室射頻

的代碼,我有,我怎麼能簡明地修改環路這些文件夾:

""" 
Clean oldFolders 
""" 
def cleanOldFolders(self): 
    pathString = self.folderRoot + '/' + self.configMode + '/' + self.appId 
    oldDirList = self.getOldDirs(pathString, 7); 
    # Notify user that the following folders are deleted 

    # remove all old dirs perhaps using shutil.removetree for each folder oldDirList, rm -Rf 

    return 

老去迪爾斯:

""" 
get all subfolders under dirPath older than olderThanDays 
""" 
def getOldDirs(self,dirPath, olderThanDays): 
    # What is the concise way of expressing Get me list of all dir/subdirs from "dirPath" that are older than "olderThanDays" 
    # I know I have to use os.walk, 
    # I want a concise loop like this - but should recurse using os.walk 
    a = [os.path.join(dirPath, myfile) for myfile in os.listdir(dirPath) 
      if (os.path.isdir(os.path.join(dirPath, myfile)) and 
       (self.isOlder(os.path.join(dirPath, myfile), olderThanDays)) 
     )] 

# for root, dirs, files in os.walk(dirPath): 
#  for name in dirs: 
#   print os.path.join(root, name) 

    return a 

回答

5

一個約os.walk()的好處之一是,它的遞歸爲您服務。對於在應用程序中的使用,指定可選關鍵字參數topdown as False很重要,因爲它的默認值是Trueos.rmdir()不會刪除非空目錄。

這意味着你的代碼需要刪除它在遇到之前的每個子目錄中的所有文件和子目錄,其中除去子目錄本身。爲了便於做到這一點,目錄列表getOldDirs()返回的順序應該是子目錄需要刪除的順序。

需要注意的是,在以下內容中,目錄的年齡是以小數而不是整數天計算的,這意味着秒數和只有6天,23小時和59秒之間的數據纔會被放入列表中,即使距離足夠長的時間只有兩秒鐘,也不會被刪除。

import os 
import time 

def getOldDirs(self, dirPath, olderThanDays): 
    """ 
    return a list of all subfolders under dirPath older than olderThanDays 
    """ 
    olderThanDays *= 86400 # convert days to seconds 
    present = time.time() 
    for root, dirs, files in os.walk(dirPath, topdown=False): 
     for name in dirs: 
      subDirPath = os.path.join(root, name) 
      if (present - os.path.getmtime(subDirPath)) > olderThanDays: 
       yield subDirPath 
3

這應該

import os 
from time import time as _time 

SEVEN_DAYS = 60*60*24*7 

def get_old_dirs(dir_path, older_than=SEVEN_DAYS): 
    time_now = _time() 
    for path, folders, files in os.walk(dir_path): 
     for folder in folders: 
      folder_path = os.path.join(path, folder) 
      if (time_now - os.path.getmtime(folder_path)) > older_than: 
       yield folder_path 

list_of_folders = list(get_old_dirs("/some/path")) 

此外,如果您不想步入大於的文件夾天(因爲你要刪除它們),你可以從folders列表

def get_old_dirs(dir_path, older_than=SEVEN_DAYS): 
    time_now = _time() 
    for path, folders, files in os.walk(dir_path): 
     for folder in folders[:]: 
      folder_path = os.path.join(path, folder) 
      if (time_now - os.path.getmtime(folder_path)) > older_than: 
       yield folder_path 
       folders.remove(folder) 
+0

+1爲第一種方法。第二種方法也很有趣,但可以通過用set()替換'old_folders'並放棄處理的文件夾來改進。這是什麼套:) – jadkik94

+0

謝謝 - 我剛剛幾分鐘前將它改爲一套;) – GP89

+0

下一個編輯更好:) – jadkik94

0

它使用os.walk,並讓你7天

舊文件的列表修剪搜索樹被刪除的文件夾名稱
import os 
from datetime import date 

old_dirs = [] 
today = date.today() 

for root, dirs, files in os.walk(start_path): 
    for name in dirs: 
     filedate = date.fromtimestamp(os.path.getmtime(os.path.join(root, name))) 
     if (today - filedate).days > 7:            
      old_dirs.append(name) 
相關問題