簡體   English   中英

如何從目錄中獲取最新的文件?

[英]How can I get the most recent file from a directory?

cached_folder = os.path.expandvars(r'%APPDATA%\Microsoft\Windows\Themes\CachedFiles\*')
list_of_files = glob.glob(cached_folder)
latest_file = max(list_of_files, key=os.path.getctime)

我只想要 CachedFiles 中最新文件的路徑,盡管這不起作用並且list_of_files是一個空列表。

我在這里做錯了什么?

下面的代碼(基本上是包裝你的代碼)對我有用。 您可能有一個空的%APPDATA%\Microsoft\Windows\Themes\CachedFiles

#!/usr/bin/env python

def main():
    import os, glob
    cached_folder = os.path.expandvars(r'%APPDATA%\Microsoft\Windows\Themes\CachedFiles\*')
    list_of_files = glob.glob(cached_folder)
    print( list_of_files )
    latest_file = max(list_of_files, key=os.path.getctime)
    print( latest_file )
    return

if (__name__ == '__main__' ) :
    main()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM