自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

求求你,別用 Os.Path 了

開發(fā) 開發(fā)工具
學(xué)習(xí)了一番 pathlib 之后,發(fā)現(xiàn)這是一個非常高效便捷的工具,用它來處理文件系統(tǒng)路徑相關(guān)的操作最合適不過,集成了很多快捷的功能,提升你的編程效率,那是妥妥的。

 [[398653]]

前段時間,在使用新版本的 Django 時,我發(fā)現(xiàn)了 settings.py 的第一行代碼從

  1. import os 
  2. BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 

變成了

  1. from pathlib import Path 
  2. BASE_DIR = Path(__file__).resolve().parent.parent 

于是我就好奇,os 和 pathlib 同樣是標準庫,為什么 pathlib 得到了 Django 的青睞?學(xué)習(xí)了一番 pathlib 之后,發(fā)現(xiàn)這是一個非常高效便捷的工具,用它來處理文件系統(tǒng)路徑相關(guān)的操作最合適不過,集成了很多快捷的功能,提升你的編程效率,那是妥妥的。

接下來讓一起看一下,為什么 pathlib 更值得我們使用。

pathlib vs os

話不多說,先看下使用對比:比如說

打印當前的路徑:

使用 os:

  1. In [13]: import os 
  2.  
  3. In [14]: os.getcwd() 
  4. Out[14]: '/Users/aaron' 

使用 pathlib:

  1. In [15]: from pathlib import Path 
  2.  
  3. In [16]: Path.cwd() 
  4. Out[16]: PosixPath('/Users/aaron'
  5. In [17]: print(Path.cwd()) 
  6. /Users/aaron 

使用 print 打印的結(jié)果是一樣的,但 os.getcwd() 返回的是字符串,而 Path.cwd() 返回的是 PosixPath 類,你還可以對此路徑進行后續(xù)的操作,會很方便。

判斷路徑是否存在:

使用 os:

  1. In [18]: os.path.exists("/Users/aaron/tmp"
  2.  
  3. Out[18]: True 

使用 pathlib:

  1. In [21]: tmp = Path("/Users/aaron/tmp"
  2.  
  3. In [22]: tmp.exists() 
  4. Out[22]: True 

可以看出 pathlib 更易讀,更面向?qū)ο蟆?/p>

顯示文件夾的內(nèi)容

  1. In [38]: os.listdir("/Users/aaron/tmp"
  2. Out[38]: ['.DS_Store''.hypothesis''b.txt''a.txt''c.py''.ipynb_checkpoints'
  3.  
  4. In [39]: tmp.iterdir() 
  5. Out[39]: <generator object Path.iterdir at 0x7fa3f20d95f0> 
  6.  
  7. In [40]: list(tmp.iterdir()) 
  8. Out[40]: 
  9. [PosixPath('/Users/aaron/tmp/.DS_Store'), 
  10.  PosixPath('/Users/aaron/tmp/.hypothesis'), 
  11.  PosixPath('/Users/aaron/tmp/b.txt'), 
  12.  PosixPath('/Users/aaron/tmp/a.txt'), 
  13.  PosixPath('/Users/aaron/tmp/c.py'), 
  14.  PosixPath('/Users/aaron/tmp/.ipynb_checkpoints')] 

可以看出 Path().iterdir 返回的是一個生成器,這在目錄內(nèi)文件特別多的時候可以大大節(jié)省內(nèi)存,提升效率。

通配符支持

os 不支持含有通配符的路徑,但 pathlib 可以:

  1. In [45]: list(Path("/Users/aaron/tmp").glob("*.txt")) 
  2. Out[45]: [PosixPath('/Users/aaron/tmp/b.txt'), PosixPath('/Users/aaron/tmp/a.txt')] 

便捷的讀寫文件操作

這是 pathlib 特有的:

  1. f = Path('test_dir/test.txt')) 
  2. f.write_text('This is a sentence.'
  3. f.read_text() 

也可以使用 with 語句:

  1. >>> p = Path('setup.py'
  2. >>> with p.open() as f: f.readline() 
  3. ... 
  4. '#!/usr/bin/env python3\n' 

獲取文件的元數(shù)據(jù)

  1. In [56]: p = Path("/Users/aaron/tmp/c.py"
  2.  
  3. In [57]: p.stat() 
  4. Out[57]: os.stat_result(st_mode=33188, st_ino=35768389, st_dev=16777221, st_nlink=1, st_uid=501, st_gid=20, st_size=20, st_atime=1620633580, st_mtime=1620633578, st_ctime=1620633578) 
  5.  
  6. In [58]: p.parts 
  7. Out[58]: ('/''Users''aaron''tmp''c.py'
  8.  
  9. In [59]: p.parent 
  10. Out[59]: PosixPath('/Users/aaron/tmp'
  11.  
  12. In [60]: p.resolve() 
  13. Out[60]: PosixPath('/Users/aaron/tmp/c.py'
  14.  
  15. In [61]: p.exists() 
  16. Out[61]: True 
  17.  
  18. In [62]: p.is_dir() 
  19. Out[62]: False 
  20.  
  21. In [63]: p.is_file() 
  22. Out[63]: True 
  23.  
  24. In [64]: p.owner() 
  25. Out[64]: 'aaron' 
  26.  
  27. In [65]: p.group() 
  28. Out[65]: 'staff' 
  29.  
  30. In [66]: p.name 
  31. Out[66]: 'c.py' 
  32.  
  33. In [67]: p.suffix 
  34. Out[67]: '.py' 
  35.  
  36. In [68]: p.suffixes 
  37. Out[68]: ['.py'
  38.  
  39. In [69]: p.stem 
  40. Out[69]: 'c' 

路徑的連接 join

相比 os.path.join,使用一個 / 是不是更為直觀和便捷?

  1. >>> p = PurePosixPath('foo'
  2. >>> p / 'bar' 
  3. PurePosixPath('foo/bar'
  4. >>> p / PurePosixPath('bar'
  5. PurePosixPath('foo/bar'
  6. >>> 'bar' / p 
  7. PurePosixPath('bar/foo'

當然,也可以使用 joinpath 方法

  1. >>> PurePosixPath('/etc').joinpath('passwd'
  2. PurePosixPath('/etc/passwd'
  3. >>> PurePosixPath('/etc').joinpath(PurePosixPath('passwd')) 
  4. PurePosixPath('/etc/passwd'
  5. >>> PurePosixPath('/etc').joinpath('init.d''apache2'
  6. PurePosixPath('/etc/init.d/apache2'
  7. >>> PureWindowsPath('c:').joinpath('/Program Files'
  8. PureWindowsPath('c:/Program Files'

路徑匹配

  1. >>> PurePath('a/b.py').match('*.py'
  2. True 
  3. >>> PurePath('/a/b/c.py').match('b/*.py'
  4. True 
  5. >>> PurePath('/a/b/c.py').match('a/*.py'
  6. False 

pathlib 出現(xiàn)的背景和要解決的問題

pathlib 目的是提供一個簡單的類層次結(jié)構(gòu)來處理文件系統(tǒng)的路徑,同時提供路徑相關(guān)的常見操作。那為什么不使用 os 模塊或者 os.path 來實現(xiàn)呢?

許多人更喜歡使用 datetime 模塊提供的高級對象來處理日期和時間,而不是使用數(shù)字時間戳和 time 模塊 API。同樣的原因,假如使用專用類表示文件系統(tǒng)路徑,也會更受歡迎。

換句話說,os.path 是面向過程風(fēng)格的,而 pathlib 是面向?qū)ο箫L(fēng)格的。Python 也在一直在慢慢地從復(fù)制 C 語言的 API 轉(zhuǎn)變?yōu)閲@各種常見功能提供更好,更有用的抽象。

其他方面,使用專用的類處理特定的需求也是很有必要的,例如 Windows 路徑不區(qū)分大小寫。

在這樣的背景下,pathlib 在 Python 3.4 版本加入標準庫。

pathlib 的優(yōu)勢和劣勢分別是什么

pathlib 的優(yōu)勢在于考慮了 Windows 路徑的特殊性,同時提供了帶 I/O 操作的和不帶 I/O 操作的類,使用場景更加明確,API 調(diào)用更加易懂。

先看下 pathlib 對類的劃分:

圖中的箭頭表示繼承自,比如 Path 繼承自 PurePath,PurePath 表示純路徑類,只提供路徑常見的操作,但不包括實際 I/O 操作,相對安全;Path 包含 PurePath 的全部功能,包括 I/O 操作。

PurePath 有兩個子類,一個是 PureWindowsPath,表示 Windows 下的路徑,不區(qū)分大小寫,另一個是 PurePosixPath,表示其他系統(tǒng)的路徑。有了 PureWindowsPath,你可以這樣對路徑進行比較:

  1. from pathlib import PureWindowsPath 
  2. >>> PureWindowsPath('a') == PureWindowsPath('A'
  3. True 

PurePath 可以在任何操作系統(tǒng)上實例化,也就是說與平臺無關(guān),你可以在 unix 系統(tǒng)上使用 PureWindowsPath,也可以在 Windows 系統(tǒng)上使用 PurePosixPath,他們還可以相互比較。

  1. >>> from pathlib import PurePosixPath, PureWindowsPath, PosixPath   
  2. >>> PurePosixPath('a') == PurePosixPath('b'
  3. False 
  4. >>> PurePosixPath('a') < PurePosixPath('b'
  5. True 
  6. >>> PurePosixPath('a') == PosixPath('a'
  7. True 
  8. >>> PurePosixPath('a') == PureWindowsPath('a'
  9. False 

可以看出,同一個類可以相互比較,不同的類比較的結(jié)果是 False。

相反,包含 I/O 操作的類 PosixPath 及 WindowsPath 只能在對應(yīng)的平臺實例化:

  1. In [8]: from pathlib import PosixPath,WindowsPath 
  2.  
  3. In [9]: PosixPath('a'
  4. Out[9]: PosixPath('a'
  5.  
  6. In [10]: WindowsPath('a'
  7. --------------------------------------------------------------------------- 
  8. NotImplementedError                       Traceback (most recent call last
  9. <ipython-input-10-cc7a0d86d4ed> in <module> 
  10. ----> 1 WindowsPath('a') 
  11.  
  12. /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pathlib.py in __new__(cls, *args, **kwargs) 
  13.    1038         self = cls._from_parts(args, init=False
  14.    1039         if not self._flavour.is_supported: 
  15. -> 1040             raise NotImplementedError("cannot instantiate %r on your system" 
  16.    1041                                       % (cls.__name__,)) 
  17.    1042         self._init() 
  18.  
  19. NotImplementedError: cannot instantiate 'WindowsPath' on your system 
  20.  
  21. In [11]: 

要說劣勢,如果有的話,那就是在選擇類時會比較困惑,到底用哪一個呢?其實如果你不太確定的話,用 Path 就可以了,這也是它的名稱最短的原因,因為更加常用,短點的名稱編寫的更快。

適用的場景

如果要處理文件系統(tǒng)相關(guān)的操作,選 pathlib 就對了。

一些關(guān)鍵點

獲取家目錄:

  1. In [70]: from pathlib import Path 
  2.  
  3. In [71]: Path.home() 
  4. Out[71]: PosixPath('/Users/aaron'

父目錄的層級獲?。?/p>

  1. >>> p = PureWindowsPath('c:/foo/bar/setup.py'
  2. >>> p.parents[0] 
  3. PureWindowsPath('c:/foo/bar'
  4. >>> p.parents[1] 
  5. PureWindowsPath('c:/foo'
  6. >>> p.parents[2] 
  7. PureWindowsPath('c:/'

獲取多個文件后綴:

  1. >>> PurePosixPath('my/library.tar.gar').suffixes 
  2. ['.tar''.gar'
  3. >>> PurePosixPath('my/library.tar.gz').suffixes 
  4. ['.tar''.gz'
  5. >>> PurePosixPath('my/library').suffixes 
  6. [] 

Windows 風(fēng)格轉(zhuǎn) Posix:

  1. >>> p = PureWindowsPath('c:\\windows'
  2. >>> str(p) 
  3. 'c:\\windows' 
  4. >>> p.as_posix() 
  5. 'c:/windows' 

獲取文件的 uri:

  1. >>> p = PurePosixPath('/etc/passwd'
  2. >>> p.as_uri() 
  3. 'file:///etc/passwd' 
  4. >>> p = PureWindowsPath('c:/Windows'
  5. >>> p.as_uri() 
  6. 'file:///c:/Windows' 

判斷是否絕對路徑:

  1. >>> PurePosixPath('/a/b').is_absolute() 
  2. True 
  3. >>> PurePosixPath('a/b').is_absolute() 
  4. False 
  5.  
  6. >>> PureWindowsPath('c:/a/b').is_absolute() 
  7. True 
  8. >>> PureWindowsPath('/a/b').is_absolute() 
  9. False 
  10. >>> PureWindowsPath('c:').is_absolute() 
  11. False 
  12. >>> PureWindowsPath('//some/share').is_absolute() 
  13. True 

文件名若有變化:

  1. >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz'
  2. >>> p.with_name('setup.py'
  3. PureWindowsPath('c:/Downloads/setup.py'

是不是非常方便?

技術(shù)的底層原理和關(guān)鍵實現(xiàn)

pathlib 并不是基于 str 的實現(xiàn),而是基于 object 設(shè)計的,這樣就嚴格地區(qū)分了 Path 對象和字符串對象,同時也用到了一點 os 的功能,比如 os.name,os.getcwd 等,這一點大家可以看 pathlib 的源碼了解更多。

最后的話

本文分享了 pathlib 的用法,后面要處理路徑相關(guān)的操作時,你應(yīng)該第一時間想到 pathlib,不會用沒有關(guān)系,搜索引擎所搜索 pathlib 就可以看到具體的使用方法。

雖然 pathlib 比 os 庫更高級,更方便并且提供了很多便捷的功能,但是我們?nèi)匀恍枰廊绾问褂?os 庫,因為 os 庫是 Python 中功能最強大且最基本的庫之一,但是,在需要一些文件系統(tǒng)操作時,強烈建議使用 pathlib。

本文轉(zhuǎn)載自微信公眾號「Python七號」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系Python七號公眾號。

 

責(zé)任編輯:武曉燕 來源: Python七號
相關(guān)推薦

2020-12-11 09:24:19

Elasticsear存儲數(shù)據(jù)

2023-11-13 22:47:58

PythonPathlib

2020-12-15 08:06:45

waitnotifyCondition

2021-09-24 09:30:05

os.path模塊Python

2020-06-15 08:12:51

try catch代碼處理器

2022-10-27 21:34:28

數(shù)據(jù)庫機器學(xué)習(xí)架構(gòu)

2020-09-22 09:05:45

MySQLUTF-8utf8mb4

2020-11-09 08:22:29

程序員 IT科技

2025-02-10 08:05:03

2010-03-25 12:50:45

Python代碼

2020-10-12 10:45:44

nullava程序員

2020-05-09 10:18:31

Java開源工具

2024-04-29 08:32:21

os.path模塊Python內(nèi)置函數(shù)

2023-12-08 14:37:51

接口jar包開發(fā)

2024-03-14 08:15:18

COUNT(*)數(shù)據(jù)庫LIMIT 1?

2020-04-08 17:53:40

TypeScriptJavaScript代碼

2021-09-30 06:13:36

打印日志error

2020-12-04 10:05:00

Pythonprint代碼

2020-12-02 11:18:50

print調(diào)試代碼Python

2021-03-05 22:57:25

遞歸閉包 Python
點贊
收藏

51CTO技術(shù)棧公眾號