新聞中心
pathlib —- 面向?qū)ο蟮奈募到y(tǒng)路徑
3.4 新版功能.

源代碼 Lib/pathlib.py
該模塊提供表示文件系統(tǒng)路徑的類,其語(yǔ)義適用于不同的操作系統(tǒng)。路徑類被分為提供純計(jì)算操作而沒(méi)有 I/O 的 純路徑,以及從純路徑繼承而來(lái)但提供 I/O 操作的 具體路徑。
如果以前從未用過(guò)此模塊,或不確定哪個(gè)類適合完成任務(wù),那要用的可能就是 Path。它在運(yùn)行代碼的平臺(tái)上實(shí)例化為 具體路徑。
在一些用例中純路徑很有用,例如:
-
如果你想要在 Unix 設(shè)備上操作 Windows 路徑(或者相反)。你不應(yīng)在 Unix 上實(shí)例化一個(gè) WindowsPath,但是你可以實(shí)例化 PureWindowsPath。
-
你只想操作路徑但不想實(shí)際訪問(wèn)操作系統(tǒng)。在這種情況下,實(shí)例化一個(gè)純路徑是有用的,因?yàn)樗鼈儧](méi)有任何訪問(wèn)操作系統(tǒng)的操作。
參見(jiàn)
PEP 428:pathlib 模塊 — 面向?qū)ο蟮奈募到y(tǒng)路徑。
參見(jiàn)
對(duì)于底層的路徑字符串操作,你也可以使用 os.path 模塊。
基礎(chǔ)使用
導(dǎo)入主類:
>>> from pathlib import Path
列出子目錄:
>>> p = Path('.')>>> [x for x in p.iterdir() if x.is_dir()][PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),PosixPath('__pycache__'), PosixPath('build')]
列出當(dāng)前目錄樹(shù)下的所有 python 源代碼文件:
>>> list(p.glob('**/*.py'))[PosixPath('test_pathlib.py'), PosixPath('setup.py'),PosixPath('pathlib.py'), PosixPath('docs/conf.py'),PosixPath('build/lib/pathlib.py')]
在目錄樹(shù)中移動(dòng):
>>> p = Path('/etc')>>> q = p / 'init.d' / 'reboot'>>> qPosixPath('/etc/init.d/reboot')>>> q.resolve()PosixPath('/etc/rc.d/init.d/halt')
查詢路徑的屬性:
>>> q.exists()True>>> q.is_dir()False
打開(kāi)一個(gè)文件:
>>> with q.open() as f: f.readline()...'#!/bin/bash\n'
純路徑
純路徑對(duì)象提供了不實(shí)際訪問(wèn)文件系統(tǒng)的路徑處理操作。有三種方式來(lái)訪問(wèn)這些類,也是不同的風(fēng)格:
class pathlib.PurePath(\pathsegments*)
一個(gè)通用的類,代表當(dāng)前系統(tǒng)的路徑風(fēng)格(實(shí)例化為 PurePosixPath 或者 PureWindowsPath):
>>> PurePath('setup.py') # Running on a Unix machinePurePosixPath('setup.py')
每一個(gè) pathsegments 的元素可能是一個(gè)代表路徑片段的字符串,一個(gè)返回字符串的實(shí)現(xiàn)了 os.PathLike 接口的對(duì)象,或者另一個(gè)路徑對(duì)象:
>>> PurePath('foo', 'some/path', 'bar')PurePosixPath('foo/some/path/bar')>>> PurePath(Path('foo'), Path('bar'))PurePosixPath('foo/bar')
當(dāng) pathsegments 為空的時(shí)候,假定為當(dāng)前目錄:
>>> PurePath()PurePosixPath('.')
當(dāng)給出一些絕對(duì)路徑,最后一位將被當(dāng)作錨(模仿 os.path.join() 的行為):
>>> PurePath('/etc', '/usr', 'lib64')PurePosixPath('/usr/lib64')>>> PureWindowsPath('c:/Windows', 'd:bar')PureWindowsPath('d:bar')
但是,在 Windows 路徑中,改變本地根目錄并不會(huì)丟棄之前盤符的設(shè)置:
>>> PureWindowsPath('c:/Windows', '/Program Files')PureWindowsPath('c:/Program Files')
假斜杠和單個(gè)點(diǎn)號(hào)會(huì)被消除,但雙點(diǎn)號(hào) ('..') 和打頭的雙斜杠 ('//') 不會(huì),因?yàn)檫@會(huì)出于各種原因改變路徑的實(shí)際含義 (例如符號(hào)鏈接、UNC 路徑等):
>>> PurePath('foo//bar')PurePosixPath('foo/bar')>>> PurePath('//foo/bar')PurePosixPath('//foo/bar')>>> PurePath('foo/./bar')PurePosixPath('foo/bar')>>> PurePath('foo/../bar')PurePosixPath('foo/../bar')
(一個(gè)很 na?ve 的做法是讓 PurePosixPath('foo/../bar') 等同于 PurePosixPath('bar'),如果 foo 是一個(gè)指向其他目錄的符號(hào)鏈接那么這個(gè)做法就將出錯(cuò))
純路徑對(duì)象實(shí)現(xiàn)了 os.PathLike 接口,允許它們?cè)谌魏谓邮艽私涌诘牡胤绞褂谩?/p>
在 3.6 版更改: 添加了 os.PathLike 接口支持。
class pathlib.PurePosixPath(\pathsegments*)
一個(gè) PurePath 的子類,路徑風(fēng)格不同于 Windows 文件系統(tǒng):
>>> PurePosixPath('/etc')PurePosixPath('/etc')
pathsegments 參數(shù)的指定和 PurePath 相同。
class pathlib.PureWindowsPath(\pathsegments*)
PurePath 的一個(gè)子類,此路徑風(fēng)格代表 Windows 文件系統(tǒng)路徑,包括 UNC paths#UNC):
>>> PureWindowsPath('c:/Program Files/')PureWindowsPath('c:/Program Files')>>> PureWindowsPath('//server/share/file')PureWindowsPath('//server/share/file')
pathsegments 參數(shù)的指定和 PurePath 相同。
無(wú)論你正運(yùn)行什么系統(tǒng),你都可以實(shí)例化這些類,因?yàn)樗鼈兲峁┑牟僮鞑蛔鋈魏蜗到y(tǒng)調(diào)用。
通用性質(zhì)
路徑是不可變并可哈希的。相同風(fēng)格的路徑可以排序與比較。這些性質(zhì)尊重對(duì)應(yīng)風(fēng)格的大小寫轉(zhuǎn)換語(yǔ)義:
>>> PurePosixPath('foo') == PurePosixPath('FOO')False>>> PureWindowsPath('foo') == PureWindowsPath('FOO')True>>> PureWindowsPath('FOO') in { PureWindowsPath('foo') }True>>> PureWindowsPath('C:') < PureWindowsPath('d:')True
不同風(fēng)格的路徑比較得到不等的結(jié)果并且無(wú)法被排序:
>>> PureWindowsPath('foo') == PurePosixPath('foo')False>>> PureWindowsPath('foo') < PurePosixPath('foo')Traceback (most recent call last):File "", line 1, in TypeError: '<' not supported between instances of 'PureWindowsPath' and 'PurePosixPath'
運(yùn)算符
斜杠 / 操作符有助于創(chuàng)建子路徑,就像 os.path.join() 一樣:
>>> p = PurePath('/etc')>>> pPurePosixPath('/etc')>>> p / 'init.d' / 'apache2'PurePosixPath('/etc/init.d/apache2')>>> q = PurePath('bin')>>> '/usr' / qPurePosixPath('/usr/bin')
文件對(duì)象可用于任何接受 os.PathLike 接口實(shí)現(xiàn)的地方。
>>> import os>>> p = PurePath('/etc')>>> os.fspath(p)'/etc'
路徑的字符串表示法為它自己原始的文件系統(tǒng)路徑(以原生形式,例如在 Windows 下使用反斜杠)。你可以傳遞給任何需要字符串形式路徑的函數(shù)。
>>> p = PurePath('/etc')>>> str(p)'/etc'>>> p = PureWindowsPath('c:/Program Files')>>> str(p)'c:\\Program Files'
類似地,在路徑上調(diào)用 bytes 將原始文件系統(tǒng)路徑作為字節(jié)對(duì)象給出,就像被 os.fsencode() 編碼一樣:
>>> bytes(p)b'/etc'
備注
只推薦在 Unix 下調(diào)用 bytes。在 Windows, unicode 形式是文件系統(tǒng)路徑的規(guī)范表示法。
訪問(wèn)個(gè)別部分
為了訪問(wèn)路徑獨(dú)立的部分 (組件),使用以下特征屬性:
PurePath.parts
一個(gè)元組,可以訪問(wèn)路徑的多個(gè)組件:
>>> p = PurePath('/usr/bin/Python3')>>> p.parts('/', 'usr', 'bin', 'python3')>>> p = PureWindowsPath('c:/Program Files/PSF')>>> p.parts('c:\\', 'Program Files', 'PSF')
(注意盤符和本地根目錄是如何重組的)
方法和特征屬性
純路徑提供以下方法和特征屬性:
PurePath.drive
一個(gè)表示驅(qū)動(dòng)器盤符或命名的字符串,如果存在:
>>> PureWindowsPath('c:/Program Files/').drive'c:'>>> PureWindowsPath('/Program Files/').drive''>>> PurePosixPath('/etc').drive''
UNC 分享也被認(rèn)作驅(qū)動(dòng)器:
>>> PureWindowsPath('//host/share/foo.txt').drive'\\\\host\\share'
PurePath.root
一個(gè)表示(本地或全局)根的字符串,如果存在:
>>> PureWindowsPath('c:/Program Files/').root'\\'>>> PureWindowsPath('c:Program Files/').root''>>> PurePosixPath('/etc').root'/'
UNC 分享一樣擁有根:
>>> PureWindowsPath('//host/share').root'\\'
如果路徑以超過(guò)兩個(gè)連續(xù)斜框打頭,PurePosixPath 會(huì)合并它們:
>>> PurePosixPath('//etc').root'//'>>> PurePosixPath('///etc').root'/'>>> PurePosixPath('////etc').root'/'
備注
This behavior conforms to The Open Group Base Specifications Issue 6, paragraph 4.11 Pathname Resolution:
“以連續(xù)兩個(gè)斜杠打頭的路徑名可能會(huì)以具體實(shí)現(xiàn)所定義的方式被解讀,但是兩個(gè)以上的前綴斜杠則應(yīng)當(dāng)被當(dāng)作一個(gè)斜杠來(lái)處理?!?/em>
PurePath.anchor
驅(qū)動(dòng)器和根的聯(lián)合:
>>> PureWindowsPath('c:/Program Files/').anchor'c:\\'>>> PureWindowsPath('c:Program Files/').anchor'c:'>>> PurePosixPath('/etc').anchor'/'>>> PureWindowsPath('//host/share').anchor'\\\\host\\share\\'
PurePath.parents
提供訪問(wèn)此路徑的邏輯祖先的不可變序列:
>>> p = PureWindowsPath('c:/foo/bar/setup.py')>>> p.parents[0]PureWindowsPath('c:/foo/bar')>>> p.parents[1]PureWindowsPath('c:/foo')>>> p.parents[2]PureWindowsPath('c:/')
在 3.10 版更改: parents 序列現(xiàn)在支持 切片 和負(fù)的索引值。
PurePath.parent
此路徑的邏輯父路徑:
>>> p = PurePosixPath('/a/b/c/d')>>> p.parentPurePosixPath('/a/b/c')
你不能超過(guò)一個(gè) anchor 或空路徑:
>>> p = PurePosixPath('/')>>> p.parentPurePosixPath('/')>>> p = PurePosixPath('.')>>> p.parentPurePosixPath('.')
備注
這是一個(gè)單純的詞法操作,因此有以下行為:
>>> p = PurePosixPath('foo/..')>>> p.parentPurePosixPath('foo')
If you want to walk an arbitrary filesystem path upwards, it is recommended to first call Path.resolve() so as to resolve symlinks and eliminate ".." components.
PurePath.name
一個(gè)表示最后路徑組件的字符串,排除了驅(qū)動(dòng)器與根目錄,如果存在的話:
>>> PurePosixPath('my/library/setup.py').name'setup.py'
UNC 驅(qū)動(dòng)器名不被考慮:
>>> PureWindowsPath('//some/share/setup.py').name'setup.py'>>> PureWindowsPath('//some/share').name''
PurePath.suffix
最后一個(gè)組件的文件擴(kuò)展名,如果存在:
>>> PurePosixPath('my/library/setup.py').suffix'.py'>>> PurePosixPath('my/library.tar.gz').suffix'.gz'>>> PurePosixPath('my/library').suffix''
PurePath.suffixes
路徑的文件擴(kuò)展名列表:
>>> PurePosixPath('my/library.tar.gar').suffixes['.tar', '.gar']>>> PurePosixPath('my/library.tar.gz').suffixes['.tar', '.gz']>>> PurePosixPath('my/library').suffixes[]
PurePath.stem
最后一個(gè)路徑組件,除去后綴:
>>> PurePosixPath('my/library.tar.gz').stem'library.tar'>>> PurePosixPath('my/library.tar').stem'library'>>> PurePosixPath('my/library').stem'library'
PurePath.as_posix()
返回使用正斜杠(/)的路徑字符串:
>>> p = PureWindowsPath('c:\\windows')>>> str(p)'c:\\windows'>>> p.as_posix()'c:/windows'
PurePath.as_uri()
將路徑表示為 file URL。如果并非絕對(duì)路徑,拋出 ValueError。
>>> p = PurePosixPath('/etc/passwd')>>> p.as_uri()'file:///etc/passwd'>>> p = PureWindowsPath('c:/Windows')>>> p.as_uri()'file:///c:/Windows'
PurePath.is_absolute()
返回此路徑是否為絕對(duì)路徑。如果路徑同時(shí)擁有驅(qū)動(dòng)器符與根路徑(如果風(fēng)格允許)則將被認(rèn)作絕對(duì)路徑。
>>> PurePosixPath('/a/b').is_absolute()True>>> PurePosixPath('a/b').is_absolute()False>>> PureWindowsPath('c:/a/b').is_absolute()True>>> PureWindowsPath('/a/b').is_absolute()False>>> PureWindowsPath('c:').is_absolute()False>>> PureWindowsPath('//some/share').is_absolute()True
PurePath.is_relative_to(\other*)
返回此路徑是否相對(duì)于 other 的路徑。
>>> p = PurePath('/etc/passwd')>>> p.is_relative_to('/etc')True>>> p.is_relative_to('/usr')False
3.9 新版功能.
PurePath.is_reserved()
在 PureWindowsPath,如果路徑是被 Windows 保留的則返回 True,否則 False。在 PurePosixPath,總是返回 False。
>>> PureWindowsPath('nul').is_reserved()True>>> PurePosixPath('nul').is_reserved()False
當(dāng)保留路徑上的文件系統(tǒng)被調(diào)用,則可能出現(xiàn)玄學(xué)失敗或者意料之外的效應(yīng)。
PurePath.joinpath(\other*)
調(diào)用此方法等同于將每個(gè) other 參數(shù)中的項(xiàng)目連接在一起:
>>> PurePosixPath('/etc').joinpath('passwd')PurePosixPath('/etc/passwd')>>> PurePosixPath('/etc').joinpath(PurePosixPath('passwd'))PurePosixPath('/etc/passwd')>>> PurePosixPath('/etc').joinpath('init.d', 'apache2')PurePosixPath('/etc/init.d/apache2')>>> PureWindowsPath('c:').joinpath('/Program Files')PureWindowsPath('c:/Program Files')
PurePath.match(pattern)
將此路徑與提供的通配符風(fēng)格的模式匹配。如果匹配成功則返回 True,否則返回 False。
如果 pattern 是相對(duì)的,則路徑可以是相對(duì)路徑或絕對(duì)路徑,并且匹配是從右側(cè)完成的:
>>> PurePath('a/b.py').match('*.py')True>>> PurePath('/a/b/c.py').match('b/*.py')True>>> PurePath('/a/b/c.py').match('a/*.py')False
如果 pattern 是絕對(duì)的,則路徑必須是絕對(duì)的,并且路徑必須完全匹配:
>>> PurePath('/a.py').match('/*.py')True>>> PurePath('a/b.py').match('/*.py')False
與其他方法一樣,是否大小寫敏感遵循平臺(tái)的默認(rèn)規(guī)則:
>>> PurePosixPath('b.py').match('*.PY')False>>> PureWindowsPath('b.py').match('*.PY')True
PurePath.relative_to(\other*)
計(jì)算此路徑相對(duì) other 表示路徑的版本。如果不可計(jì)算,則拋出 ValueError:
>>> p = PurePosixPath('/etc/passwd')>>> p.relative_to('/')PurePosixPath('etc/passwd')>>> p.relative_to('/etc')PurePosixPath('passwd')>>> p.relative_to('/usr')Traceback (most recent call last):File "", line 1, in File "pathlib.py", line 694, in relative_to.format(str(self), str(formatted)))ValueError: '/etc/passwd' is not in the subpath of '/usr' OR one path is relative and the other absolute.
注意:此函數(shù)是 PurePath 的一部分并且適用于字符串。 它不會(huì)檢查或訪問(wèn)下層的文件結(jié)構(gòu)。
PurePath.with_name(name)
返回一個(gè)新的路徑并修改 name。如果原本路徑?jīng)]有 name,ValueError 被拋出:
>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')>>> p.with_name('setup.py')PureWindowsPath('c:/Downloads/setup.py')>>> p = PureWindowsPath('c:/')>>> p.with_name('setup.py')Traceback (most recent call last):File "", line 1, in File "/home/antoine/cpython/default/Lib/pathlib.py", line 751, in with_nameraise ValueError("%r has an empty name" % (self,))ValueError: PureWindowsPath('c:/') has an empty name
PurePath.with_stem(stem)
返回一個(gè)帶有修改后 stem 的新路徑。 如果原路徑?jīng)]有名稱,則會(huì)引發(fā) ValueError:
>>> p = PureWindowsPath('c:/Downloads/draft.txt')>>> p.with_stem('final')PureWindowsPath('c:/Downloads/final.txt')>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')>>> p.with_stem('lib')PureWindowsPath('c:/Downloads/lib.gz')>>> p = PureWindowsPath('c:/')>>> p.with_stem('')Traceback (most recent call last):File "", line 1, in File "/home/antoine/cpython/default/Lib/pathlib.py", line 861, in with_stemreturn self.with_name(stem + self.suffix)File "/home/antoine/cpython/default/Lib/pathlib.py", line 851, in with_nameraise ValueError("%r has an empty name" % (self,))ValueError: PureWindowsPath('c:/') has an empty name
3.9 新版功能.
PurePath.with_suffix(suffix)
返回一個(gè)新的路徑并修改 suffix。如果原本的路徑?jīng)]有后綴,新的 suffix 則被追加以代替。如果 suffix 是空字符串,則原本的后綴被移除:
>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')>>> p.with_suffix('.bz2')PureWindowsPath('c:/Downloads/pathlib.tar.bz2')>>> p = PureWindowsPath('README')>>> p.with_suffix('.txt')PureWindowsPath('README.txt')>>> p = PureWindowsPath('README.txt')>>> p.with_suffix('')PureWindowsPath('README')
具體路徑
具體路徑是純路徑的子類。除了后者提供的操作之外,它們還提供了對(duì)路徑對(duì)象進(jìn)行系統(tǒng)調(diào)用的方法。有三種方法可以實(shí)例化具體路徑:
class pathlib.Path(\pathsegments*)
一個(gè) PurePath 的子類,此類以當(dāng)前系統(tǒng)的路徑風(fēng)格表示路徑(實(shí)例化為 PosixPath 或 WindowsPath):
>>> Path('setup.py')PosixPath('setup.py')
pathsegments 參數(shù)的指定和 PurePath 相同。
class pathlib.PosixPath(\pathsegments*)
一個(gè) Path 和 PurePosixPath 的子類,此類表示一個(gè)非 Windows 文件系統(tǒng)的具體路徑:
>>> PosixPath('/etc')PosixPath('/etc')
pathsegments 參數(shù)的指定和 PurePath 相同。
class pathlib.WindowsPath(\pathsegments*)
Path 和 PureWindowsPath 的子類,從類表示一個(gè) Windows 文件系統(tǒng)的具體路徑:
>>> WindowsPath('c:/Program Files/')WindowsPath('c:/Program Files')
pathsegments 參數(shù)的指定和 PurePath 相同。
你只能實(shí)例化與當(dāng)前系統(tǒng)風(fēng)格相同的類(允許系統(tǒng)調(diào)用作用于不兼容的路徑風(fēng)格可能在應(yīng)用程序中導(dǎo)致缺陷或失敗):
>>> import os>>> os.name'posix'>>> Path('setup.py')PosixPath('setup.py')>>> PosixPath('setup.py')PosixPath('setup.py')>>> WindowsPath('setup.py')Traceback (most recent call last):File "", line 1, in File "pathlib.py", line 798, in __new__% (cls.__name__,))NotImplementedError: cannot instantiate 'WindowsPath' on your system
方法
除純路徑方法外,實(shí)體路徑還提供以下方法。 如果系統(tǒng)調(diào)用失?。ɡ缫?yàn)槁窂讲淮嬖冢┻@些方法中許多都會(huì)引發(fā) OSError。
在 3.8 版更改: 對(duì)于包含 OS 層級(jí)無(wú)法表示字符的路徑,exists(), is_dir(), is_file(), is_mount(), is_symlink(), is_block_device(), is_char_device(), is_fifo(), is_socket() 現(xiàn)在將返回 False 而不是引發(fā)異常。
classmethod Path.cwd()
返回一個(gè)新的表示當(dāng)前目錄的路徑對(duì)象(和 os.getcwd() 返回的相同):
>>> Path.cwd()PosixPath('/home/antoine/pathlib')
classmethod Path.home()
返回一個(gè)表示用戶家目錄的新路徑對(duì)象(與帶 ~ 構(gòu)造的 os.path.expanduser() 所返回的相同)。 如果無(wú)法解析家目錄,則會(huì)引發(fā) RuntimeError。
>>> Path.home()PosixPath('/home/antoine')
3.5 新版功能.
Path.stat(**, follow_symlinks=True*)
返回一個(gè) os.stat_result 對(duì)象,其中包含有關(guān)此路徑的信息,例如 os.stat()。 結(jié)果會(huì)在每次調(diào)用此方法時(shí)重新搜索。
此方法通常會(huì)跟隨符號(hào)鏈接;要對(duì) symlink 使用 stat 請(qǐng)?zhí)砑訁?shù) follow_symlinks=False,或者使用 lstat()。
>>> p = Path('setup.py')>>> p.stat().st_size956>>> p.stat().st_mtime1327883547.852554
在 3.10 版更改: 增加了 follow_symlinks 形參。
Path.chmod(mode, **, follow_symlinks=True*)
改變文件模式和權(quán)限,和 os.chmod() 一樣。
此方法通常會(huì)跟隨符號(hào)鏈接。 某些 Unix 變種支持改變 symlink 本身的權(quán)限;在這些平臺(tái)上你可以添加參數(shù) follow_symlinks=False,或者使用 lchmod()。
>>> p = Path('setup.py')>>> p.stat().st_mode33277>>> p.chmod(0o444)>>> p.stat().st_mode33060
在 3.10 版更改: 增加了 follow_symlinks 形參。
Path.exists()
此路徑是否指向一個(gè)已存在的文件或目錄:
>>> Path('.').exists()True>>> Path('setup.py').exists()True>>> Path('/etc').exists()True>>> Path('nonexistentfile').exists()False
備注
如果路徑指向一個(gè)符號(hào)鏈接, exists() 返回此符號(hào)鏈接是否指向存在的文件或目錄。
Path.expanduser()
返回帶有擴(kuò)展 ~ 和 ~user 構(gòu)造的新路徑,與 os.path.expanduser() 所返回的相同。 如果無(wú)法解析家目錄,則會(huì)引發(fā) RuntimeError。
>>> p = PosixPath('~/films/Monty Python')>>> p.expanduser()PosixPath('/home/eric/films/Monty Python')
3.5 新版功能.
Path.glob(pattern)
解析相對(duì)于此路徑的通配符 pattern,產(chǎn)生所有匹配的文件:
>>> sorted(Path('.').glob('*.py'))[PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')]>>> sorted(Path('.').glob('*/*.py'))[PosixPath('docs/conf.py')]
pattern 的形式與 fnmatch 的相同,還增加了 “**“ 表示 “此目錄以及所有子目錄,遞歸”。 換句話說(shuō),它啟用遞歸通配:
>>> sorted(Path('.').glob('**/*.py'))[PosixPath('build/lib/pathlib.py'),PosixPath('docs/conf.py'),PosixPath('pathlib.py'),PosixPath('setup.py'),PosixPath('test_pathlib.py')]
備注
在一個(gè)較大的目錄樹(shù)中使用 “**“ 模式可能會(huì)消耗非常多的時(shí)間。
引發(fā)一個(gè) 審計(jì)事件 pathlib.Path.glob 附帶參數(shù) self, pattern。
在 3.11 版更改: Return only directories if pattern ends with a pathname components separator (sep or altsep).
Path.group()
返回?fù)碛写宋募挠脩艚M。如果文件的 GID 無(wú)法在系統(tǒng)數(shù)據(jù)庫(kù)中找到,將拋出 KeyError 。
Path.is_dir()
如果路徑指向一個(gè)目錄(或者一個(gè)指向目錄的符號(hào)鏈接)則返回 True,如果指向其他類型的文件則返回 False。
當(dāng)路徑不存在或者是一個(gè)破損的符號(hào)鏈接時(shí)也會(huì)返回 False;其他錯(cuò)誤(例如權(quán)限錯(cuò)誤)被傳播。
Path.is_file()
如果路徑指向一個(gè)正常的文件(或者一個(gè)指向正常文件的符號(hào)鏈接)則返回 True,如果指向其他類型的文件則返回 False。
當(dāng)路徑不存在或者是一個(gè)破損的符號(hào)鏈接時(shí)也會(huì)返回 False;其他錯(cuò)誤(例如權(quán)限錯(cuò)誤)被傳播。
Path.is_mount()
如果路徑是一個(gè) 掛載點(diǎn) path/.. 是否處于一個(gè)和 path 不同的設(shè)備中,或者 file:path/.. 和 path 是否指向相同設(shè)備的相同 i-node —— 這能檢測(cè)所有 Unix 以及 POSIX 變種上的掛載點(diǎn)。 Windows 上未實(shí)現(xiàn)。
3.7 新版功能.
Path.is_symlink()
如果路徑指向符號(hào)鏈接則返回 True, 否則 False。
如果路徑不存在也返回 False;其他錯(cuò)誤(例如權(quán)限錯(cuò)誤)被傳播。
Path.is_socket()
如果路徑指向一個(gè) Unix socket 文件(或者指向 Unix socket 文件的符號(hào)鏈接)則返回 True,如果指向其他類型的文件則返回 False。
當(dāng)路徑不存在或者是一個(gè)破損的符號(hào)鏈接時(shí)也會(huì)返回 False;其他錯(cuò)誤(例如權(quán)限錯(cuò)誤)被傳播。
Path.is_fifo()
如果路徑指向一個(gè)先進(jìn)先出存儲(chǔ)(或者指向先進(jìn)先出存儲(chǔ)的符號(hào)鏈接)則返回 True ,指向其他類型的文件則返回 False。
當(dāng)路徑不存在或者是一個(gè)破損的符號(hào)鏈接時(shí)也會(huì)返回 False;其他錯(cuò)誤(例如權(quán)限錯(cuò)誤)被傳播。
Path.is_block_device()
如果文件指向一個(gè)塊設(shè)備(或者指向塊設(shè)備的符號(hào)鏈接)則返回 True,指向其他類型的文件則返回 False。
當(dāng)路徑不存在或者是一個(gè)破損的符號(hào)鏈接時(shí)也會(huì)返回 False;其他錯(cuò)誤(例如權(quán)限錯(cuò)誤)被傳播。
Path.is_char_device()
如果路徑指向一個(gè)字符設(shè)備(或指向字符設(shè)備的符號(hào)鏈接)則返回 True,指向其他類型的文件則返回 False。
當(dāng)路徑不存在或者是一個(gè)破損的符號(hào)鏈接時(shí)也會(huì)返回 False;其他錯(cuò)誤(例如權(quán)限錯(cuò)誤)被傳播。
Path.iterdir()
當(dāng)路徑指向一個(gè)目錄時(shí),產(chǎn)生該路徑下的對(duì)象的路徑:
>>> p = Path('docs')>>> for child in p.iterdir(): child...PosixPath('docs/conf.py')PosixPath('docs/_templates')PosixPath('docs/make.bat')PosixPath('docs/index.rst')PosixPath('docs/_build')PosixPath('docs/_static')PosixPath('docs/Makefile')
子條目會(huì)以任意順序生成,并且不包括特殊條目 '.' 和 '..'。 如果迭代器創(chuàng)建之后有文件在目錄中被移除或添加,是否要包括該文件所對(duì)應(yīng)的路徑對(duì)象并沒(méi)有明確規(guī)定。
Path.lchmod(mode)
就像 Path.chmod() 但是如果路徑指向符號(hào)鏈接則是修改符號(hào)鏈接的模式,而不是修改符號(hào)鏈接的目標(biāo)。
Path.lstat()
就和 Path.stat() 一樣,但是如果路徑指向符號(hào)鏈接,則是返回符號(hào)鏈接而不是目標(biāo)的信息。
Path.mkdir(mode=0o777, parents=False, exist_ok=False
當(dāng)前名稱:創(chuàng)新互聯(lián)Python教程:pathlib—-面向?qū)ο蟮奈募到y(tǒng)路徑
分享路徑:http://m.5511xx.com/article/djdjhph.html


咨詢
建站咨詢
