pathlib — 物件導向的檔案系統路徑 — 你所不知道的 Python 標準函式庫用法 12

pathlib – 物件導向的檔案系統路徑

This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which provide purely computational operations without I/O, and concrete paths, which inherit from pure paths but also provide I/O operations.

Source code: Lib/


01. Quickstart Tutorial

使用 pathlib.Path 來表示一個路徑,對於 Path 物件,我們可以透過 / 來串接路徑:

我們可以對 Path 物件進行一連串的操作:

對於 Path 物件,我們也能使用 glob 來搜尋檔案:

glob 出來的路徑,同樣也是 Path 物件,也能對他們使用上述的操作。

我們也能對 Path 物件修改他的 Unix 權限:

Path 物件也有提供一些常用的功能,例如說:

02. HOW-TO Guides


03. Discussions


04. References

  • 11.1. pathlib — Object-oriented filesystem paths
  • PEP 428 — The pathlib module — object-oriented filesystem paths



Leave a reply:

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.