获取超过x天的所有文件夹(os.walk),删除

时间:2012-12-07 18:36:39

标签: python

如何简洁地表达“获取超过x天的所有文件夹”

我有一个方法getOldDirs(dirPath,olderThanDays),它必须遍历给定的根文件夹并返回一个比7天更早的文件夹列表。

我从另一个函数cleanOldFolders()调用上面的函数。 cleanOldFolders()将删除类似于“rm -Rf

的文件夹

我拥有的代码,如何简洁地修改循环:

"""
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

3 个答案:

答案 0 :(得分:5)

关于os.walk()的一个好处是它为你做了递归。对于在您的应用程序中的使用,将{em> 自上而下 指定为False非常重要,因为它的默认值为Trueos.rmdir()不会删除非空目录。

这意味着您的代码需要在删除子目录之前删除它遇到的每个子目录中的所有文件和子目录。为了便于这样做,目录列表getOldDirs()返回应该按照需要删除子目录的顺序。

同样重要的是要注意,在下面的内容中,目录的年龄是以小数而非整数天计算的,这意味着秒计数和那个只说6天23小时59秒的计数' t被列入要删除的列表,即使它距离足够老还只有两秒钟。

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

答案 1 :(得分: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"))

此外,如果您不想走进超过older_than天的文件夹(因为您要删除它们),您可以修剪搜索树,从{{1删除文件夹名称列表

folders

答案 2 :(得分: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)