如何在 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()”函式根據“get_creation_time()”函式返回的值進行排序。
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()”函式是獲取目錄內容的更有效且更好的替代方案,優於“os.listdir()”。然後,我們可以使用“sorted()”和一個自定義鍵函式(使用“os.stat()”獲取每個條目的建立時間)。
示例
在本示例中,我們使用“os.scandir()”函式以及“sorted()”來獲取 Python 中按建立時間排序的目錄列表。
“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
使用 pathlib.Path.glob() 與 sorted() 和 stat()
來自“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”模組提供的內建函式提供了便利性和易用性。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP