您如何在 Python 中按建立日期對目錄列表進行排序?
在 Python 中管理檔案和目錄時,經常會出現需要按建立日期對目錄列表進行排序的情況。按建立日期對檔案和目錄進行排序的任務對於各種目的都是有益的,比如分析最近新增的檔案或基於時間順序組織資料。Python 具有多種方法和技術可以高效實現此目標。透過使用“os”模組、“pathlib”模組或第三方庫,您可以輕鬆地按建立日期獲取排序的目錄列表。
在這篇詳盡的文章中,我們將繼續探討在 Python 中按建立日期對目錄列表進行排序的不同方法。我們還將提供分步說明和程式碼示例來指導您完成此過程。無論您喜歡使用“os”模組、“pathlib”模組還是像“sortedcontainers”這樣的外部庫,本文都將為您提供有效導航和組織目錄內容的工具。
讓我們使用 Python 開啟這趟檔案目錄探索之旅,並深入瞭解如何根據建立日期獲取一個已排序的檔案目錄列表!
使用 os.listdir() 與 sorted() 和 os.path.getctime()
“os.listdir()” 函式允許我們獲取某個給定目錄中的專案列表(檔案和目錄)。透過使用“sorted()”函式和一個自定義鍵函式來獲取每個專案的建立日期,我們可以實現根據建立日期對檔案目錄進行排序。
示例
首先,在下面給出的程式碼中,匯入“os”模組。
“sorted_directory_listing_by_creation_time_with_os_listdir()”函式獲取“directory”作為輸入,並使用“os.listdir()”、“sorted()”和“os.path.getctime()”來返回根據建立日期對檔案目錄進行排序。
“get_creation_time()”函式定義在主函式中。此函式獲取一個“item”(檔案或目錄名稱),並使用“os.path.getctime()”返回該專案的建立日期。
“os.listdir(directory)”用於獲取指定目錄中的專案列表(檔案和目錄)。
專案列表已按“sorted()”函式返回的值進行排序。
import os
def sorted_directory_listing_by_creation_time_with_os_listdir(directory):
def get_creation_time(item):
item_path = os.path.join(directory, item)
return os.path.getctime(item_path)
items = os.listdir(directory)
sorted_items = sorted(items, key=get_creation_time)
return sorted_items
使用 os.scandir() 與 sorted() 和 stat()
我們發現“os.scandir()”函式是一個更加高效且更好的選擇,可用於獲取目錄內容。然後我們可以使用“sorted()”和一個自定義鍵函式,該函式使用“os.stat()”獲取每個條目的建立日期。
示例
在這個示例中,我們使用 Python 中的“os.scandir()”函式和“sorted()”來獲取根據建立日期排序的檔案目錄列表。
“sorted_directory_listing_by_creation_time_with_os_scandir()”函式獲取“directory”作為輸入,並使用“os.scandir()”、“sorted()”和“os.stat()”來返回經過排序的檔案目錄列表。
我們隨後在主函式中定義“get_creation_time()”函式。此函式接受一個“entry”(檔案目錄物件)並使用“entry.stat().st_ctime”輸出該條目的建立日期。
此處,“with”語句用於確保在塊執行後正確清理資源。
我們在“sorted()”中使用 lambda 函式作為“key”引數,該引數要求排序應基於“get_creation_time()”函式返回的值。
import os
def sorted_directory_listing_by_creation_time_with_os_scandir(directory):
def get_creation_time(entry):
return entry.stat().st_ctime
with os.scandir(directory) as entries:
sorted_entries = sorted(entries, key=get_creation_time)
sorted_items = [entry.name for entry in sorted_entries]
return sorted_items
使用 pathlib.Path.iterdir() 與 sorted() 和 stat()
我們發現“pathlib”模組提供了一種相對現代且便捷的方式來管理檔案路徑。我們使用“sorted()”和一個自定義鍵函式,該函式使用“os.stat()”來檢索每個專案的建立日期。
示例
此處,我們從“pathlib”模組中匯入“Path”類。
“sorted_directory_listing_by_creation_time_with_pathlib_iterdir()”函式接受“directory”作為輸入,並使用“Path.iterdir()”、“sorted()”和“os.stat()”來返回經過排序的檔案目錄列表。
我們繼續在主函式中定義“get_creation_time()”函式。此函式獲取一個“item”(檔案或目錄物件),並使用“item.stat().st_ctime”輸出該專案的建立日期。
然後使用“Path(directory)”建立一個“Path”物件,其中“directory”為輸入目錄。
“path_object.iterdir()”函式用於獲取指定目錄中的專案(檔案和目錄)的迭代器。
“sorted()”函式然後基於“get_creation_time()”函式返回的值對專案列表進行排序。
from pathlib import Path
def sorted_directory_listing_by_creation_time_with_pathlib_iterdir(directory):
def get_creation_time(item):
return item.stat().st_ctime
path_object = Path(directory)
items = path_object.iterdir()
sorted_items = sorted(items, key=get_creation_time)
return [item.name for item in sorted_items]
使用外部庫 sortedcontainers
在效率和效能至關重要的用例中,我們可以選擇使用“sortedcontainers”等外部庫來獲取基於建立日期的有序目錄列表。
示例
現在,您已瞭解了“sorted_directory_listing_by_creation_time_with_sortedcontainers()" 函式展示如何使用“sortedcontainers”庫來獲取基於建立日期的有序目錄列表。
首先,使用“pip install sortedcontainers”安裝“sortedcontainers”庫。
該函式以“directory”為輸入,並使用“sortedcontainers”的“SortedList”類和“os.path.getctime()”,返回有序的目錄列表。
“os.listdir(directory)”用於獲取指定目錄中的專案(檔案和目錄)列表。
我們使用“items”列表和 lambda 函式(作為“key”引數)初始化“SortedList”類,該函式指定按“os.path.getctime()”返回的每個專案的建立日期進行排序。
import os
from sortedcontainers import SortedList
def sorted_directory_listing_by_creation_time_with_sortedcontainers(directory):
items = os.listdir(directory)
sorted_items = SortedList(items, key=lambda item: os.path.getctime(os.path.join(directory, item)))
return sorted_items
使用帶 sorted() 和 stat() 的 pathlib.Path.glob()
“pathlib”模組中的“Path.glob()”方法允許我們獲取給定目錄中專案(檔案和目錄)的迭代器。然後,我們繼續使用“sorted()”和一個使用“os.stat()”獲取每個專案建立日期的自定義鍵函式。
示例
首先,匯入“pathlib”模組中的“Path”類;它表示檔案系統路徑。
眾所周知,“sorted_directory_listing_by_creation_time_with_pathlib_glob()”函式以“directory”作為輸入,並使用“Path.glob()”、“sorted()”和“os.stat()”,返回有序的目錄列表。
“get_creation_time()”函式在主函式中定義。此函式採用“item”(檔案或目錄物件),並使用“item.stat().st_ctime”,輸出該專案的建立日期。
接下來,我們使用“Path(directory)”建立一個“Path”物件,其中“directory”是輸入目錄。
“path_object.glob('*')”用於獲取指定目錄中的專案(檔案和目錄)的迭代器。
“sorted()”函式然後基於“get_creation_time()”函式返回的值對專案列表進行排序。
from pathlib import Path
def sorted_directory_listing_by_creation_time_with_pathlib_glob(directory):
def get_creation_time(item):
return item.stat().st_ctime
path_object = Path(directory)
items = path_object.glob('*')
sorted_items = sorted(items, key=get_creation_time)
return [item.name for item in sorted_items]
總之,獲取按建立日期排序的目錄列表,在 Python 中是一項強大的技能,可增強有效的文件管理和組織。我們在本文中探討了實現此目標的多種方法和技巧,包括使用“os.listdir()”、“os.scandir()”、“pathlib.Path.iterdir()”,外部庫“sortedcontainers”和“pathlib.Path.glob()”。
如果您喜歡“os”模組的簡單性或“pathlib”模組的現代方法,Python 提供了靈活、高效的方法,用來基於建立日期瀏覽和排序目錄內容。
精通這些技術,將使您能夠構建穩健的應用程式,用來處理檔案系統、資料分析和許多其他需要高效的目錄列表排序的任務。
正確的方法的選擇取決於您專案特定的要求。如果效能是主要考慮因素,則“sortedcontainers”等外部庫可能是您更好的選擇。另一方面,“os”和“pathlib”模組提供的內建函式提供了方便性和易用性。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP