我怎麼能簡潔地表達「得到比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
+1爲第一種方法。第二種方法也很有趣,但可以通過用set()替換'old_folders'並放棄處理的文件夾來改進。這是什麼套:) – jadkik94
謝謝 - 我剛剛幾分鐘前將它改爲一套;) – GP89
下一個編輯更好:) – jadkik94