2009-03-04 34 views

回答

2

這樣做會的Python的方式:

fileInfos = list(DirectoryInfo(path).GetFiles()) 
fileInfos.sort(key=lambda f: f.CreationTime, reverse=True) 

列表的排序方法採用的是被用來獲取每個項目的排序鍵按鍵功能。

1

DirectoryInfo.GetFiles()返回一個FileInfo對象數組。我創建了一個通用列表來存放FileInfo objs,並使用自定義比較器進行排序。

logDir = r"C:\temp" 
fileInfosArray = DirectoryInfo(logDir).GetFiles("*.log") 
fileInfosList = List[FileInfo]() 
fileInfosList.AddRange(fileInfosArray) 
fileInfosList.Sort(FileInfoCompareCreationTimeDesc) 
for fileInfo in fileInfosList: 
    print fileInfo.CreationTime, fileInfo.LastAccessTime, fileInfo.LastWriteTime, fileInfo.Name 

# comparison delegate for FileInfo objects: sort by CreationTime Descending 
def FileInfoCompareCreationTimeDesc(fileInfo1, fileInfo2): 
    return fileInfo2.CreationTime.CompareTo(fileInfo1.CreationTime)