日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:pathlib—-面向?qū)ο蟮奈募到y(tǒng)路徑

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í)例化為 具體路徑。

在一些用例中純路徑很有用,例如:

  1. 如果你想要在 Unix 設(shè)備上操作 Windows 路徑(或者相反)。你不應(yīng)在 Unix 上實(shí)例化一個(gè) WindowsPath,但是你可以實(shí)例化 PureWindowsPath。

  2. 你只想操作路徑但不想實(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)入主類:

 
 
 
 
  1. >>> from pathlib import Path

列出子目錄:

 
 
 
 
  1. >>> p = Path('.')
  2. >>> [x for x in p.iterdir() if x.is_dir()]
  3. [PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),
  4. PosixPath('__pycache__'), PosixPath('build')]

列出當(dāng)前目錄樹(shù)下的所有 python 源代碼文件:

 
 
 
 
  1. >>> list(p.glob('**/*.py'))
  2. [PosixPath('test_pathlib.py'), PosixPath('setup.py'),
  3. PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
  4. PosixPath('build/lib/pathlib.py')]

在目錄樹(shù)中移動(dòng):

 
 
 
 
  1. >>> p = Path('/etc')
  2. >>> q = p / 'init.d' / 'reboot'
  3. >>> q
  4. PosixPath('/etc/init.d/reboot')
  5. >>> q.resolve()
  6. PosixPath('/etc/rc.d/init.d/halt')

查詢路徑的屬性:

 
 
 
 
  1. >>> q.exists()
  2. True
  3. >>> q.is_dir()
  4. False

打開(kāi)一個(gè)文件:

 
 
 
 
  1. >>> with q.open() as f: f.readline()
  2. ...
  3. '#!/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):

 
 
 
 
  1. >>> PurePath('setup.py') # Running on a Unix machine
  2. PurePosixPath('setup.py')

每一個(gè) pathsegments 的元素可能是一個(gè)代表路徑片段的字符串,一個(gè)返回字符串的實(shí)現(xiàn)了 os.PathLike 接口的對(duì)象,或者另一個(gè)路徑對(duì)象:

 
 
 
 
  1. >>> PurePath('foo', 'some/path', 'bar')
  2. PurePosixPath('foo/some/path/bar')
  3. >>> PurePath(Path('foo'), Path('bar'))
  4. PurePosixPath('foo/bar')

當(dāng) pathsegments 為空的時(shí)候,假定為當(dāng)前目錄:

 
 
 
 
  1. >>> PurePath()
  2. PurePosixPath('.')

當(dāng)給出一些絕對(duì)路徑,最后一位將被當(dāng)作錨(模仿 os.path.join() 的行為):

 
 
 
 
  1. >>> PurePath('/etc', '/usr', 'lib64')
  2. PurePosixPath('/usr/lib64')
  3. >>> PureWindowsPath('c:/Windows', 'd:bar')
  4. PureWindowsPath('d:bar')

但是,在 Windows 路徑中,改變本地根目錄并不會(huì)丟棄之前盤符的設(shè)置:

 
 
 
 
  1. >>> PureWindowsPath('c:/Windows', '/Program Files')
  2. PureWindowsPath('c:/Program Files')

假斜杠和單個(gè)點(diǎn)號(hào)會(huì)被消除,但雙點(diǎn)號(hào) ('..') 和打頭的雙斜杠 ('//') 不會(huì),因?yàn)檫@會(huì)出于各種原因改變路徑的實(shí)際含義 (例如符號(hào)鏈接、UNC 路徑等):

 
 
 
 
  1. >>> PurePath('foo//bar')
  2. PurePosixPath('foo/bar')
  3. >>> PurePath('//foo/bar')
  4. PurePosixPath('//foo/bar')
  5. >>> PurePath('foo/./bar')
  6. PurePosixPath('foo/bar')
  7. >>> PurePath('foo/../bar')
  8. 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):

 
 
 
 
  1. >>> PurePosixPath('/etc')
  2. PurePosixPath('/etc')

pathsegments 參數(shù)的指定和 PurePath 相同。

class pathlib.PureWindowsPath(\pathsegments*)

PurePath 的一個(gè)子類,此路徑風(fēng)格代表 Windows 文件系統(tǒng)路徑,包括 UNC paths#UNC):

 
 
 
 
  1. >>> PureWindowsPath('c:/Program Files/')
  2. PureWindowsPath('c:/Program Files')
  3. >>> PureWindowsPath('//server/share/file')
  4. 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ǔ)義:

 
 
 
 
  1. >>> PurePosixPath('foo') == PurePosixPath('FOO')
  2. False
  3. >>> PureWindowsPath('foo') == PureWindowsPath('FOO')
  4. True
  5. >>> PureWindowsPath('FOO') in { PureWindowsPath('foo') }
  6. True
  7. >>> PureWindowsPath('C:') < PureWindowsPath('d:')
  8. True

不同風(fēng)格的路徑比較得到不等的結(jié)果并且無(wú)法被排序:

 
 
 
 
  1. >>> PureWindowsPath('foo') == PurePosixPath('foo')
  2. False
  3. >>> PureWindowsPath('foo') < PurePosixPath('foo')
  4. Traceback (most recent call last):
  5. File "", line 1, in
  6. TypeError: '<' not supported between instances of 'PureWindowsPath' and 'PurePosixPath'

運(yùn)算符

斜杠 / 操作符有助于創(chuàng)建子路徑,就像 os.path.join() 一樣:

 
 
 
 
  1. >>> p = PurePath('/etc')
  2. >>> p
  3. PurePosixPath('/etc')
  4. >>> p / 'init.d' / 'apache2'
  5. PurePosixPath('/etc/init.d/apache2')
  6. >>> q = PurePath('bin')
  7. >>> '/usr' / q
  8. PurePosixPath('/usr/bin')

文件對(duì)象可用于任何接受 os.PathLike 接口實(shí)現(xiàn)的地方。

 
 
 
 
  1. >>> import os
  2. >>> p = PurePath('/etc')
  3. >>> os.fspath(p)
  4. '/etc'

路徑的字符串表示法為它自己原始的文件系統(tǒng)路徑(以原生形式,例如在 Windows 下使用反斜杠)。你可以傳遞給任何需要字符串形式路徑的函數(shù)。

 
 
 
 
  1. >>> p = PurePath('/etc')
  2. >>> str(p)
  3. '/etc'
  4. >>> p = PureWindowsPath('c:/Program Files')
  5. >>> str(p)
  6. 'c:\\Program Files'

類似地,在路徑上調(diào)用 bytes 將原始文件系統(tǒng)路徑作為字節(jié)對(duì)象給出,就像被 os.fsencode() 編碼一樣:

 
 
 
 
  1. >>> bytes(p)
  2. b'/etc'

備注

只推薦在 Unix 下調(diào)用 bytes。在 Windows, unicode 形式是文件系統(tǒng)路徑的規(guī)范表示法。

訪問(wèn)個(gè)別部分

為了訪問(wèn)路徑獨(dú)立的部分 (組件),使用以下特征屬性:

PurePath.parts

一個(gè)元組,可以訪問(wèn)路徑的多個(gè)組件:

 
 
 
 
  1. >>> p = PurePath('/usr/bin/Python3')
  2. >>> p.parts
  3. ('/', 'usr', 'bin', 'python3')
  4. >>> p = PureWindowsPath('c:/Program Files/PSF')
  5. >>> p.parts
  6. ('c:\\', 'Program Files', 'PSF')

(注意盤符和本地根目錄是如何重組的)

方法和特征屬性

純路徑提供以下方法和特征屬性:

PurePath.drive

一個(gè)表示驅(qū)動(dòng)器盤符或命名的字符串,如果存在:

 
 
 
 
  1. >>> PureWindowsPath('c:/Program Files/').drive
  2. 'c:'
  3. >>> PureWindowsPath('/Program Files/').drive
  4. ''
  5. >>> PurePosixPath('/etc').drive
  6. ''

UNC 分享也被認(rèn)作驅(qū)動(dòng)器:

 
 
 
 
  1. >>> PureWindowsPath('//host/share/foo.txt').drive
  2. '\\\\host\\share'

PurePath.root

一個(gè)表示(本地或全局)根的字符串,如果存在:

 
 
 
 
  1. >>> PureWindowsPath('c:/Program Files/').root
  2. '\\'
  3. >>> PureWindowsPath('c:Program Files/').root
  4. ''
  5. >>> PurePosixPath('/etc').root
  6. '/'

UNC 分享一樣擁有根:

 
 
 
 
  1. >>> PureWindowsPath('//host/share').root
  2. '\\'

如果路徑以超過(guò)兩個(gè)連續(xù)斜框打頭,PurePosixPath 會(huì)合并它們:

 
 
 
 
  1. >>> PurePosixPath('//etc').root
  2. '//'
  3. >>> PurePosixPath('///etc').root
  4. '/'
  5. >>> PurePosixPath('////etc').root
  6. '/'

備注

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)合:

 
 
 
 
  1. >>> PureWindowsPath('c:/Program Files/').anchor
  2. 'c:\\'
  3. >>> PureWindowsPath('c:Program Files/').anchor
  4. 'c:'
  5. >>> PurePosixPath('/etc').anchor
  6. '/'
  7. >>> PureWindowsPath('//host/share').anchor
  8. '\\\\host\\share\\'

PurePath.parents

提供訪問(wèn)此路徑的邏輯祖先的不可變序列:

 
 
 
 
  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:/')

在 3.10 版更改: parents 序列現(xiàn)在支持 切片 和負(fù)的索引值。

PurePath.parent

此路徑的邏輯父路徑:

 
 
 
 
  1. >>> p = PurePosixPath('/a/b/c/d')
  2. >>> p.parent
  3. PurePosixPath('/a/b/c')

你不能超過(guò)一個(gè) anchor 或空路徑:

 
 
 
 
  1. >>> p = PurePosixPath('/')
  2. >>> p.parent
  3. PurePosixPath('/')
  4. >>> p = PurePosixPath('.')
  5. >>> p.parent
  6. PurePosixPath('.')

備注

這是一個(gè)單純的詞法操作,因此有以下行為:

 
 
 
 
  1. >>> p = PurePosixPath('foo/..')
  2. >>> p.parent
  3. PurePosixPath('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)器與根目錄,如果存在的話:

 
 
 
 
  1. >>> PurePosixPath('my/library/setup.py').name
  2. 'setup.py'

UNC 驅(qū)動(dòng)器名不被考慮:

 
 
 
 
  1. >>> PureWindowsPath('//some/share/setup.py').name
  2. 'setup.py'
  3. >>> PureWindowsPath('//some/share').name
  4. ''

PurePath.suffix

最后一個(gè)組件的文件擴(kuò)展名,如果存在:

 
 
 
 
  1. >>> PurePosixPath('my/library/setup.py').suffix
  2. '.py'
  3. >>> PurePosixPath('my/library.tar.gz').suffix
  4. '.gz'
  5. >>> PurePosixPath('my/library').suffix
  6. ''

PurePath.suffixes

路徑的文件擴(kuò)展名列表:

 
 
 
 
  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. []

PurePath.stem

最后一個(gè)路徑組件,除去后綴:

 
 
 
 
  1. >>> PurePosixPath('my/library.tar.gz').stem
  2. 'library.tar'
  3. >>> PurePosixPath('my/library.tar').stem
  4. 'library'
  5. >>> PurePosixPath('my/library').stem
  6. 'library'

PurePath.as_posix()

返回使用正斜杠(/)的路徑字符串:

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

PurePath.as_uri()

將路徑表示為 file URL。如果并非絕對(duì)路徑,拋出 ValueError。

 
 
 
 
  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'

PurePath.is_absolute()

返回此路徑是否為絕對(duì)路徑。如果路徑同時(shí)擁有驅(qū)動(dòng)器符與根路徑(如果風(fēng)格允許)則將被認(rèn)作絕對(duì)路徑。

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

PurePath.is_relative_to(\other*)

返回此路徑是否相對(duì)于 other 的路徑。

 
 
 
 
  1. >>> p = PurePath('/etc/passwd')
  2. >>> p.is_relative_to('/etc')
  3. True
  4. >>> p.is_relative_to('/usr')
  5. False

3.9 新版功能.

PurePath.is_reserved()

在 PureWindowsPath,如果路徑是被 Windows 保留的則返回 True,否則 False。在 PurePosixPath,總是返回 False

 
 
 
 
  1. >>> PureWindowsPath('nul').is_reserved()
  2. True
  3. >>> PurePosixPath('nul').is_reserved()
  4. False

當(dāng)保留路徑上的文件系統(tǒng)被調(diào)用,則可能出現(xiàn)玄學(xué)失敗或者意料之外的效應(yīng)。

PurePath.joinpath(\other*)

調(diào)用此方法等同于將每個(gè) other 參數(shù)中的項(xiàng)目連接在一起:

 
 
 
 
  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')

PurePath.match(pattern)

將此路徑與提供的通配符風(fēng)格的模式匹配。如果匹配成功則返回 True,否則返回 False。

如果 pattern 是相對(duì)的,則路徑可以是相對(duì)路徑或絕對(duì)路徑,并且匹配是從右側(cè)完成的:

 
 
 
 
  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

如果 pattern 是絕對(duì)的,則路徑必須是絕對(duì)的,并且路徑必須完全匹配:

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

與其他方法一樣,是否大小寫敏感遵循平臺(tái)的默認(rèn)規(guī)則:

 
 
 
 
  1. >>> PurePosixPath('b.py').match('*.PY')
  2. False
  3. >>> PureWindowsPath('b.py').match('*.PY')
  4. True

PurePath.relative_to(\other*)

計(jì)算此路徑相對(duì) other 表示路徑的版本。如果不可計(jì)算,則拋出 ValueError:

 
 
 
 
  1. >>> p = PurePosixPath('/etc/passwd')
  2. >>> p.relative_to('/')
  3. PurePosixPath('etc/passwd')
  4. >>> p.relative_to('/etc')
  5. PurePosixPath('passwd')
  6. >>> p.relative_to('/usr')
  7. Traceback (most recent call last):
  8. File "", line 1, in
  9. File "pathlib.py", line 694, in relative_to
  10. .format(str(self), str(formatted)))
  11. 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 被拋出:

 
 
 
 
  1. >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
  2. >>> p.with_name('setup.py')
  3. PureWindowsPath('c:/Downloads/setup.py')
  4. >>> p = PureWindowsPath('c:/')
  5. >>> p.with_name('setup.py')
  6. Traceback (most recent call last):
  7. File "", line 1, in
  8. File "/home/antoine/cpython/default/Lib/pathlib.py", line 751, in with_name
  9. raise ValueError("%r has an empty name" % (self,))
  10. ValueError: PureWindowsPath('c:/') has an empty name

PurePath.with_stem(stem)

返回一個(gè)帶有修改后 stem 的新路徑。 如果原路徑?jīng)]有名稱,則會(huì)引發(fā) ValueError:

 
 
 
 
  1. >>> p = PureWindowsPath('c:/Downloads/draft.txt')
  2. >>> p.with_stem('final')
  3. PureWindowsPath('c:/Downloads/final.txt')
  4. >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
  5. >>> p.with_stem('lib')
  6. PureWindowsPath('c:/Downloads/lib.gz')
  7. >>> p = PureWindowsPath('c:/')
  8. >>> p.with_stem('')
  9. Traceback (most recent call last):
  10. File "", line 1, in
  11. File "/home/antoine/cpython/default/Lib/pathlib.py", line 861, in with_stem
  12. return self.with_name(stem + self.suffix)
  13. File "/home/antoine/cpython/default/Lib/pathlib.py", line 851, in with_name
  14. raise ValueError("%r has an empty name" % (self,))
  15. ValueError: PureWindowsPath('c:/') has an empty name

3.9 新版功能.

PurePath.with_suffix(suffix)

返回一個(gè)新的路徑并修改 suffix。如果原本的路徑?jīng)]有后綴,新的 suffix 則被追加以代替。如果 suffix 是空字符串,則原本的后綴被移除:

 
 
 
 
  1. >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
  2. >>> p.with_suffix('.bz2')
  3. PureWindowsPath('c:/Downloads/pathlib.tar.bz2')
  4. >>> p = PureWindowsPath('README')
  5. >>> p.with_suffix('.txt')
  6. PureWindowsPath('README.txt')
  7. >>> p = PureWindowsPath('README.txt')
  8. >>> p.with_suffix('')
  9. 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):

 
 
 
 
  1. >>> Path('setup.py')
  2. PosixPath('setup.py')

pathsegments 參數(shù)的指定和 PurePath 相同。

class pathlib.PosixPath(\pathsegments*)

一個(gè) Path 和 PurePosixPath 的子類,此類表示一個(gè)非 Windows 文件系統(tǒng)的具體路徑:

 
 
 
 
  1. >>> PosixPath('/etc')
  2. PosixPath('/etc')

pathsegments 參數(shù)的指定和 PurePath 相同。

class pathlib.WindowsPath(\pathsegments*)

Path 和 PureWindowsPath 的子類,從類表示一個(gè) Windows 文件系統(tǒng)的具體路徑:

 
 
 
 
  1. >>> WindowsPath('c:/Program Files/')
  2. WindowsPath('c:/Program Files')

pathsegments 參數(shù)的指定和 PurePath 相同。

你只能實(shí)例化與當(dāng)前系統(tǒng)風(fēng)格相同的類(允許系統(tǒng)調(diào)用作用于不兼容的路徑風(fēng)格可能在應(yīng)用程序中導(dǎo)致缺陷或失敗):

 
 
 
 
  1. >>> import os
  2. >>> os.name
  3. 'posix'
  4. >>> Path('setup.py')
  5. PosixPath('setup.py')
  6. >>> PosixPath('setup.py')
  7. PosixPath('setup.py')
  8. >>> WindowsPath('setup.py')
  9. Traceback (most recent call last):
  10. File "", line 1, in
  11. File "pathlib.py", line 798, in __new__
  12. % (cls.__name__,))
  13. 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() 返回的相同):

 
 
 
 
  1. >>> Path.cwd()
  2. PosixPath('/home/antoine/pathlib')

classmethod Path.home()

返回一個(gè)表示用戶家目錄的新路徑對(duì)象(與帶 ~ 構(gòu)造的 os.path.expanduser() 所返回的相同)。 如果無(wú)法解析家目錄,則會(huì)引發(fā) RuntimeError。

 
 
 
 
  1. >>> Path.home()
  2. 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()。

 
 
 
 
  1. >>> p = Path('setup.py')
  2. >>> p.stat().st_size
  3. 956
  4. >>> p.stat().st_mtime
  5. 1327883547.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()。

 
 
 
 
  1. >>> p = Path('setup.py')
  2. >>> p.stat().st_mode
  3. 33277
  4. >>> p.chmod(0o444)
  5. >>> p.stat().st_mode
  6. 33060

在 3.10 版更改: 增加了 follow_symlinks 形參。

Path.exists()

此路徑是否指向一個(gè)已存在的文件或目錄:

 
 
 
 
  1. >>> Path('.').exists()
  2. True
  3. >>> Path('setup.py').exists()
  4. True
  5. >>> Path('/etc').exists()
  6. True
  7. >>> Path('nonexistentfile').exists()
  8. False

備注

如果路徑指向一個(gè)符號(hào)鏈接, exists() 返回此符號(hào)鏈接是否指向存在的文件或目錄。

Path.expanduser()

返回帶有擴(kuò)展 ~~user 構(gòu)造的新路徑,與 os.path.expanduser() 所返回的相同。 如果無(wú)法解析家目錄,則會(huì)引發(fā) RuntimeError。

 
 
 
 
  1. >>> p = PosixPath('~/films/Monty Python')
  2. >>> p.expanduser()
  3. PosixPath('/home/eric/films/Monty Python')

3.5 新版功能.

Path.glob(pattern)

解析相對(duì)于此路徑的通配符 pattern,產(chǎn)生所有匹配的文件:

 
 
 
 
  1. >>> sorted(Path('.').glob('*.py'))
  2. [PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')]
  3. >>> sorted(Path('.').glob('*/*.py'))
  4. [PosixPath('docs/conf.py')]

pattern 的形式與 fnmatch 的相同,還增加了 “**“ 表示 “此目錄以及所有子目錄,遞歸”。 換句話說(shuō),它啟用遞歸通配:

 
 
 
 
  1. >>> sorted(Path('.').glob('**/*.py'))
  2. [PosixPath('build/lib/pathlib.py'),
  3. PosixPath('docs/conf.py'),
  4. PosixPath('pathlib.py'),
  5. PosixPath('setup.py'),
  6. 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) :在文件系統(tǒng)中被其他不同的文件系統(tǒng)掛載的地點(diǎn)。在 POSIX 系統(tǒng),此函數(shù)檢查 path 的父級(jí) —— 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ì)象的路徑:

 
 
 
 
  1. >>> p = Path('docs')
  2. >>> for child in p.iterdir(): child
  3. ...
  4. PosixPath('docs/conf.py')
  5. PosixPath('docs/_templates')
  6. PosixPath('docs/make.bat')
  7. PosixPath('docs/index.rst')
  8. PosixPath('docs/_build')
  9. PosixPath('docs/_static')
  10. 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