Hướng dẫn python pathlib add filename to path - python pathlib thêm tên tệp vào đường dẫn

Mã sau:

Show
from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Desktop + "/subdir"

Nhận lỗi sau:

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'

Tôi rõ ràng đang làm điều gì đó mờ ám ở đây, nhưng nó đặt ra câu hỏi: Làm cách nào để truy cập vào một thư mục con của đối tượng

>>> list(p.glob('**/*.py'))
[PosixPath('test_pathlib.py'), PosixPath('setup.py'),
 PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
 PosixPath('build/lib/pathlib.py')]
0?

Hướng dẫn python pathlib add filename to path - python pathlib thêm tên tệp vào đường dẫn

Georgy

11.1k7 Huy hiệu vàng62 Huy hiệu bạc70 Huy hiệu đồng7 gold badges62 silver badges70 bronze badges

Hỏi ngày 10 tháng 1 năm 2018 lúc 15:37Jan 10, 2018 at 15:37

3

  • Toán tử chính xác để mở rộng đối tượng
    >>> list(p.glob('**/*.py'))
    [PosixPath('test_pathlib.py'), PosixPath('setup.py'),
     PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
     PosixPath('build/lib/pathlib.py')]
    
    1 là
    >>> list(p.glob('**/*.py'))
    [PosixPath('test_pathlib.py'), PosixPath('setup.py'),
     PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
     PosixPath('build/lib/pathlib.py')]
    
    2
from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
  • Khi một số đường dẫn tuyệt đối được đưa ra, con đường cuối cùng được lấy làm mỏ neo (bắt chước hành vi ____ 83 83):
>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
  • Trong một đường dẫn Windows, việc thay đổi gốc cục bộ không loại bỏ cài đặt ổ đĩa trước đó:
>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
  • Tham khảo tài liệu để biết các chi tiết bổ sung liên quan đến việc đưa ra một đường dẫn tuyệt đối, chẳng hạn như
    >>> list(p.glob('**/*.py'))
    [PosixPath('test_pathlib.py'), PosixPath('setup.py'),
     PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
     PosixPath('build/lib/pathlib.py')]
    
    4.

Resources:

  • Pathlib
    • Sử dụng cơ bản
  • Mô -đun Pathlib của Python 3: Taming hệ thống tệp

Hướng dẫn python pathlib add filename to path - python pathlib thêm tên tệp vào đường dẫn

Đã trả lời ngày 10 tháng 1 năm 2018 lúc 15:41Jan 10, 2018 at 15:41

Ray Salemiray SalemiRay Salemi

4.5574 Huy hiệu vàng25 Huy hiệu bạc55 Huy hiệu Đồng4 gold badges25 silver badges55 bronze badges

0

Những gì bạn đang tìm kiếm là:

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Path.joinpath(Desktop, "subdir")

Hàm

>>> list(p.glob('**/*.py'))
[PosixPath('test_pathlib.py'), PosixPath('setup.py'),
 PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
 PosixPath('build/lib/pathlib.py')]
5 sẽ nối tham số thứ hai vào mục thứ nhất và thêm '/' cho bạn.

Đã trả lời ngày 10 tháng 1 năm 2018 lúc 15:43Jan 10, 2018 at 15:43

Hướng dẫn python pathlib add filename to path - python pathlib thêm tên tệp vào đường dẫn

r.ookr.ookr.ook

13.1k2 Huy hiệu vàng21 Huy hiệu bạc38 Huy hiệu đồng2 gold badges21 silver badges38 bronze badges

3

Mới trong phiên bản 3.4.

Mã nguồn: lib/pathlib.py Lib/pathlib.py


Mô -đun này cung cấp các lớp đại diện cho các đường dẫn hệ thống tập tin với ngữ nghĩa phù hợp cho các hệ điều hành khác nhau. Các lớp đường dẫn được phân chia giữa các đường dẫn thuần túy, cung cấp các hoạt động tính toán hoàn toàn mà không có I/O và các đường dẫn cụ thể, kế thừa từ các đường dẫn thuần túy nhưng cũng cung cấp các hoạt động I/O.pure paths, which provide purely computational operations without I/O, and concrete paths, which inherit from pure paths but also provide I/O operations.

Hướng dẫn python pathlib add filename to path - python pathlib thêm tên tệp vào đường dẫn

Nếu bạn không bao giờ sử dụng mô -đun này trước đây hoặc chỉ cần chắc chắn rằng lớp nào phù hợp với nhiệm vụ của bạn,

>>> list(p.glob('**/*.py'))
[PosixPath('test_pathlib.py'), PosixPath('setup.py'),
 PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
 PosixPath('build/lib/pathlib.py')]
6 rất có thể là những gì bạn cần. Nó khởi tạo một đường dẫn cụ thể cho nền tảng mà mã đang chạy.concrete path for the platform the code is running on.

Đường dẫn thuần túy là hữu ích trong một số trường hợp đặc biệt; Ví dụ:

  1. Nếu bạn muốn thao tác các đường dẫn Windows trên máy Unix (hoặc ngược lại). Bạn không thể khởi tạo

    >>> list(p.glob('**/*.py'))
    [PosixPath('test_pathlib.py'), PosixPath('setup.py'),
     PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
     PosixPath('build/lib/pathlib.py')]
    
    7 khi chạy trên Unix, nhưng bạn có thể khởi tạo
    >>> list(p.glob('**/*.py'))
    [PosixPath('test_pathlib.py'), PosixPath('setup.py'),
     PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
     PosixPath('build/lib/pathlib.py')]
    
    8.

  2. Bạn muốn đảm bảo rằng mã của bạn chỉ thao tác các đường dẫn mà không thực sự truy cập hệ điều hành. Trong trường hợp này, việc khởi tạo một trong các lớp thuần túy có thể hữu ích vì những lớp đơn giản là không có bất kỳ hoạt động truy cập hệ điều hành nào.

Xem thêm

PEP 428: Mô-đun PathLib-Đường dẫn hệ thống tập tin hướng đối tượng.: The pathlib module – object-oriented filesystem paths.

Xem thêm

PEP 428: Mô-đun PathLib-Đường dẫn hệ thống tập tin hướng đối tượng.

Đối với thao tác đường dẫn cấp thấp trên các chuỗi, bạn cũng có thể sử dụng mô-đun >>> list(p.glob('**/*.py')) [PosixPath('test_pathlib.py'), PosixPath('setup.py'), PosixPath('pathlib.py'), PosixPath('docs/conf.py'), PosixPath('build/lib/pathlib.py')] 9.

Sử dụng cơ bản

>>> from pathlib import Path

Nhập lớp chính:

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

Liệt kê các thư mục con:

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

Liệt kê các tệp nguồn Python trong cây thư mục này:

>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')

Điều hướng bên trong một cây thư mục:

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
0

Thuộc tính đường dẫn truy vấn:

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
1

Mở tệp:

Con đường thuần túy

Các đối tượng Pure Path cung cấp các hoạt động xử lý đường dẫn mà không thực sự truy cập vào một hệ thống tập tin. Có ba cách để truy cập các lớp này, mà chúng ta cũng gọi là hương vị:pathlib.PurePath(*pathsegments)

ClassPathLib.PurePath (*PathSegments) ¶

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
2

Một lớp chung đại diện cho hương vị đường dẫn hệ thống (khởi tạo nó tạo ra

>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
0 hoặc
>>> list(p.glob('**/*.py'))
[PosixPath('test_pathlib.py'), PosixPath('setup.py'),
 PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
 PosixPath('build/lib/pathlib.py')]
8):

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
3

Mỗi phần tử của PathSegments có thể là một chuỗi đại diện cho một phân đoạn đường dẫn, một đối tượng thực hiện giao diện

>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
2 trả về một chuỗi hoặc một đối tượng đường dẫn khác:

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
4

Khi một số đường dẫn tuyệt đối được đưa ra, con đường cuối cùng được lấy làm mỏ neo (bắt chước hành vi ____ 93 93):

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
5

Tuy nhiên, trong một đường dẫn Windows, việc thay đổi gốc cục bộ không loại bỏ cài đặt ổ đĩa trước đó:

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
6

Các dấu gạch chéo giả và các chấm đơn bị sụp đổ, nhưng các chấm đôi (

>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
4) và các dấu gạch chéo đôi hàng đầu (
>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
5) thì không, vì điều này sẽ thay đổi ý nghĩa của một đường dẫn vì nhiều lý do (ví dụ: liên kết tượng trưng, ​​đường dẫn UNC):

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
7

.

Các đối tượng Pure Path thực hiện giao diện

>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
2, cho phép chúng được sử dụng ở bất cứ đâu giao diện được chấp nhận.

Đã thay đổi trong phiên bản 3.6: Đã thêm hỗ trợ cho giao diện

>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
2.Added support for the
>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
2 interface.

classPathLib.PurePosixPath (*PathSegments) ¶pathlib.PurePosixPath(*pathsegments)

Một lớp con của

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
01, hương vị đường dẫn này đại diện cho các đường dẫn hệ thống tập tin không phải Windows:

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
8

PathSegments được chỉ định tương tự như

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
01.

ClassPathLib.PureWWindowsPath (*PathSegments) ¶ pathlib.PureWindowsPath(*pathsegments)

Một lớp con của

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
01, hương vị đường dẫn này đại diện cho các đường dẫn hệ thống tập tin Windows, bao gồm các đường dẫn UNC:

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
9

PathSegments được chỉ định tương tự như

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
01.

ClassPathLib.PureWWindowsPath (*PathSegments) ¶

Một lớp con của --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () 1 from pathlib import Path 2 Desktop = Path('Desktop') ----> 3 SubDeskTop = Desktop+"/subdir" TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str' 01, hương vị đường dẫn này đại diện cho các đường dẫn hệ thống tập tin Windows, bao gồm các đường dẫn UNC:

Bất kể hệ thống mà bạn đang chạy, bạn có thể khởi tạo tất cả các lớp này, vì chúng không cung cấp bất kỳ hoạt động nào thực hiện các cuộc gọi hệ thống.

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
0

Thuộc tính chung¶

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
1

Con đường là bất biến và có thể băm. Con đường của một hương vị tương tự là tương đương và có thể đặt hàng. Những tính chất này tôn trọng ngữ nghĩa gấp lại trường hợp của hương vị:

Các con đường của một hương vị khác nhau so sánh không đồng đều và không thể đặt hàng:

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
2

Người điều khiển trong

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
3

Toán tử Slash giúp tạo đường dẫn trẻ em, tương tự như

>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
3:

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
4

Một đối tượng đường dẫn có thể được sử dụng ở bất cứ đâu mà đối tượng triển khai

>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
2 được chấp nhận:

Biểu diễn chuỗi của đường dẫn là đường dẫn hệ thống tập tin thô (ở dạng gốc, ví dụ: với các dấu gạch chéo ngược trong Windows), bạn có thể chuyển đến bất kỳ chức năng nào lấy đường dẫn tệp dưới dạng chuỗi:

Tương tự, việc gọi

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
07 trên đường dẫn cung cấp đường dẫn hệ thống tập tin thô dưới dạng đối tượng byte, được mã hóa bởi
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
08:

Ghi chú

Gọi

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
07 chỉ được đề xuất theo Unix. Trong Windows, biểu mẫu Unicode là biểu diễn chính tắc của các đường dẫn hệ thống tập tin.

Truy cập các bộ phận riêng lẻparts

Để truy cập vào các bộ phận riêng lẻ (các thành phần) của một đường dẫn, hãy sử dụng thuộc tính sau:

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
5

PurePath.Parts¶

Một tuple cho phép truy cập vào các thành phần khác nhau của đường dẫn:

(Lưu ý cách ổ đĩa và gốc cục bộ được tập hợp lại trong một phần)

Phương pháp và Thuộc tínhdrive

Các đường dẫn thuần túy cung cấp các phương thức và thuộc tính sau:

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
6

Purepath.Drive¶

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
7

Một chuỗi đại diện cho ký tự hoặc tên ổ đĩa, nếu có:root

Cổ phiếu của UNC cũng được coi là ổ đĩa:

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
8

Purepath.root¶

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
9

Một chuỗi đại diện cho gốc (cục bộ hoặc toàn cầu), nếu có:

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
0

Biểu diễn chuỗi của đường dẫn là đường dẫn hệ thống tập tin thô (ở dạng gốc, ví dụ: với các dấu gạch chéo ngược trong Windows), bạn có thể chuyển đến bất kỳ chức năng nào lấy đường dẫn tệp dưới dạng chuỗi:

Tương tự, việc gọi

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
07 trên đường dẫn cung cấp đường dẫn hệ thống tập tin thô dưới dạng đối tượng byte, được mã hóa bởi
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
08:

Ghi chú

Gọi
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
07 chỉ được đề xuất theo Unix. Trong Windows, biểu mẫu Unicode là biểu diễn chính tắc của các đường dẫn hệ thống tập tin.
anchor

Truy cập các bộ phận riêng lẻ

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
1

Để truy cập vào các bộ phận riêng lẻ (các thành phần) của một đường dẫn, hãy sử dụng thuộc tính sau:parents

PurePath.Parts¶

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
2

Một tuple cho phép truy cập vào các thành phần khác nhau của đường dẫn:The parents sequence now supports slices and negative index values.

(Lưu ý cách ổ đĩa và gốc cục bộ được tập hợp lại trong một phần)parent

Phương pháp và Thuộc tính

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
3

Các đường dẫn thuần túy cung cấp các phương thức và thuộc tính sau:

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
4

Biểu diễn chuỗi của đường dẫn là đường dẫn hệ thống tập tin thô (ở dạng gốc, ví dụ: với các dấu gạch chéo ngược trong Windows), bạn có thể chuyển đến bất kỳ chức năng nào lấy đường dẫn tệp dưới dạng chuỗi:

Tương tự, việc gọi

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
07 trên đường dẫn cung cấp đường dẫn hệ thống tập tin thô dưới dạng đối tượng byte, được mã hóa bởi
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
08:

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
5

Ghi chú

Gọi
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
07 chỉ được đề xuất theo Unix. Trong Windows, biểu mẫu Unicode là biểu diễn chính tắc của các đường dẫn hệ thống tập tin.
name

Truy cập các bộ phận riêng lẻ

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
6

Để truy cập vào các bộ phận riêng lẻ (các thành phần) của một đường dẫn, hãy sử dụng thuộc tính sau:

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
7

PurePath.Parts¶suffix

Một tuple cho phép truy cập vào các thành phần khác nhau của đường dẫn:

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
8

(Lưu ý cách ổ đĩa và gốc cục bộ được tập hợp lại trong một phần)suffixes

Phương pháp và Thuộc tính

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
9

Các đường dẫn thuần túy cung cấp các phương thức và thuộc tính sau:stem

Purepath.Drive¶

>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
0

Một chuỗi đại diện cho ký tự hoặc tên ổ đĩa, nếu có:as_posix()

Cổ phiếu của UNC cũng được coi là ổ đĩa:

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

Purepath.root¶as_uri()

Một chuỗi đại diện cho gốc (cục bộ hoặc toàn cầu), nếu có:

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

Cổ phiếu của UNC luôn có gốc:is_absolute()

Nếu đường dẫn bắt đầu với nhiều hơn hai lần cắt liên tiếp,

>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
0 sẽ sụp đổ chúng:

>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
3

PurePath.is_relative_to (*Khác) ¶is_relative_to(*other)

Trả về liệu con đường này có liên quan đến đường dẫn khác hay không.

>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
4

Mới trong phiên bản 3.9.

PurePath.is_reserved () ¶is_reserved()

Với

>>> list(p.glob('**/*.py'))
[PosixPath('test_pathlib.py'), PosixPath('setup.py'),
 PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
 PosixPath('build/lib/pathlib.py')]
8, trả về
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 nếu đường dẫn được coi là dành riêng trong Windows,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu không. Với
>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
0,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 luôn được trả lại.

>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
5

Các cuộc gọi hệ thống tệp trên các đường dẫn dành riêng có thể thất bại một cách bí ẩn hoặc có các hiệu ứng ngoài ý muốn.

Purepath.joinpath (*Khác) ¶joinpath(*other)

Gọi phương thức này tương đương với việc kết hợp đường dẫn với từng đối số khác lần lượt:

>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
6

PurePath.Match (mẫu) ¶match(pattern)

Phù hợp với con đường này với mô hình kiểu Quả cầu được cung cấp. Trả lại

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu khớp thành công,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu không.

Nếu mẫu tương đối, đường dẫn có thể là tương đối hoặc tuyệt đối và kết hợp được thực hiện từ bên phải:

>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
7

Nếu mẫu là tuyệt đối, đường dẫn phải tuyệt đối và toàn bộ đường dẫn phải khớp:

>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
8

Cũng như các phương pháp khác, độ nhạy trường hợp tuân theo mặc định nền tảng:

>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
9

PurePath.Relative_to (*Khác) ¶relative_to(*other)

Tính một phiên bản của đường dẫn này so với đường dẫn được biểu thị bởi khác. Nếu nó không thể, ValueError được nâng lên:

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Path.joinpath(Desktop, "subdir")
0

Lưu ý: Chức năng này là một phần của

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
01 và hoạt động với các chuỗi. Nó không kiểm tra hoặc truy cập cấu trúc tệp cơ bản.

PurePath.With_Name (Tên) ¶with_name(name)

Trả về một đường dẫn mới với

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
24 đã thay đổi. Nếu đường dẫn ban đầu không có tên, ValueError sẽ được nêu ra:

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Path.joinpath(Desktop, "subdir")
1

PurePath.With_stem (STEM) ¶with_stem(stem)

Trả về một đường dẫn mới với

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
25 đã thay đổi. Nếu đường dẫn ban đầu không có tên, ValueError sẽ được nêu ra:

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Path.joinpath(Desktop, "subdir")
2

Mới trong phiên bản 3.9.

PurePath.is_reserved () ¶with_suffix(suffix)

Với

>>> list(p.glob('**/*.py'))
[PosixPath('test_pathlib.py'), PosixPath('setup.py'),
 PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
 PosixPath('build/lib/pathlib.py')]
8, trả về
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 nếu đường dẫn được coi là dành riêng trong Windows,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu không. Với
>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
0,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 luôn được trả lại.

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Path.joinpath(Desktop, "subdir")
3

Các cuộc gọi hệ thống tệp trên các đường dẫn dành riêng có thể thất bại một cách bí ẩn hoặc có các hiệu ứng ngoài ý muốn.

Purepath.joinpath (*Khác) ¶

Gọi phương thức này tương đương với việc kết hợp đường dẫn với từng đối số khác lần lượt: pathlib.Path(*pathsegments)

PurePath.Match (mẫu) ¶

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Path.joinpath(Desktop, "subdir")
4

Phù hợp với con đường này với mô hình kiểu Quả cầu được cung cấp. Trả lại

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu khớp thành công,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu không.

Nếu mẫu tương đối, đường dẫn có thể là tương đối hoặc tuyệt đối và kết hợp được thực hiện từ bên phải: pathlib.PosixPath(*pathsegments)

Nếu mẫu là tuyệt đối, đường dẫn phải tuyệt đối và toàn bộ đường dẫn phải khớp:

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Path.joinpath(Desktop, "subdir")
5

Phù hợp với con đường này với mô hình kiểu Quả cầu được cung cấp. Trả lại

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu khớp thành công,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu không.

Nếu mẫu tương đối, đường dẫn có thể là tương đối hoặc tuyệt đối và kết hợp được thực hiện từ bên phải: pathlib.WindowsPath(*pathsegments)

Nếu mẫu là tuyệt đối, đường dẫn phải tuyệt đối và toàn bộ đường dẫn phải khớp:

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Path.joinpath(Desktop, "subdir")
6

Phù hợp với con đường này với mô hình kiểu Quả cầu được cung cấp. Trả lại

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu khớp thành công,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu không.

Nếu mẫu tương đối, đường dẫn có thể là tương đối hoặc tuyệt đối và kết hợp được thực hiện từ bên phải:

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Path.joinpath(Desktop, "subdir")
7

Nếu mẫu là tuyệt đối, đường dẫn phải tuyệt đối và toàn bộ đường dẫn phải khớp:

Cũng như các phương pháp khác, độ nhạy trường hợp tuân theo mặc định nền tảng:

PurePath.Relative_to (*Khác) ¶Path.cwd()

Tính một phiên bản của đường dẫn này so với đường dẫn được biểu thị bởi khác. Nếu nó không thể, ValueError được nâng lên:

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Path.joinpath(Desktop, "subdir")
8

Lưu ý: Chức năng này là một phần của
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
01 và hoạt động với các chuỗi. Nó không kiểm tra hoặc truy cập cấu trúc tệp cơ bản.
Path.home()

PurePath.With_Name (Tên) ¶

from pathlib import Path
Desktop = Path('Desktop')
SubDeskTop = Path.joinpath(Desktop, "subdir")
9

Trả về một đường dẫn mới với

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
24 đã thay đổi. Nếu đường dẫn ban đầu không có tên, ValueError sẽ được nêu ra:

PurePath.With_stem (STEM) ¶stat(*, follow_symlinks=True)

Trả về một đường dẫn mới với

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
25 đã thay đổi. Nếu đường dẫn ban đầu không có tên, ValueError sẽ được nêu ra:

PurePath.With_Suffix (hậu tố) ¶

>>> from pathlib import Path
0

Trả về một đường dẫn mới với

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
26 đã thay đổi. Nếu đường dẫn ban đầu không có hậu tố, thì hậu tố mới sẽ được nối thêm. Nếu hậu tố là một chuỗi trống, hậu tố ban đầu sẽ bị loại bỏ:The follow_symlinks parameter was added.

Đường bê tông¶chmod(mode, *, follow_symlinks=True)

Các đường dẫn bê tông là các lớp con của các lớp đường dẫn thuần túy. Ngoài các hoạt động được cung cấp bởi sau này, họ cũng cung cấp các phương thức để thực hiện các cuộc gọi hệ thống trên các đối tượng đường dẫn. Có ba cách để khởi tạo các đường dẫn bê tông:

classPathlib.path (*PathSegments) ¶

>>> from pathlib import Path
1

Trả về một đường dẫn mới với

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
26 đã thay đổi. Nếu đường dẫn ban đầu không có hậu tố, thì hậu tố mới sẽ được nối thêm. Nếu hậu tố là một chuỗi trống, hậu tố ban đầu sẽ bị loại bỏ:The follow_symlinks parameter was added.

Đường bê tông¶exists()

Các đường dẫn bê tông là các lớp con của các lớp đường dẫn thuần túy. Ngoài các hoạt động được cung cấp bởi sau này, họ cũng cung cấp các phương thức để thực hiện các cuộc gọi hệ thống trên các đối tượng đường dẫn. Có ba cách để khởi tạo các đường dẫn bê tông:

>>> from pathlib import Path
2

classPathlib.path (*PathSegments) ¶

Một lớp con của

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
01, lớp này đại diện cho các đường dẫn cụ thể của hương vị đường dẫn hệ thống (khởi tạo nó tạo ra một
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
28 hoặc
>>> list(p.glob('**/*.py'))
[PosixPath('test_pathlib.py'), PosixPath('setup.py'),
 PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
 PosixPath('build/lib/pathlib.py')]
7):

PathSegments được chỉ định tương tự như
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
01.
expanduser()

classpathlib.posixpath (*pathSegments) ¶

>>> from pathlib import Path
3

Mới trong phiên bản 3.5.

Path.glob (mẫu) ¶glob(pattern)

GLOB Mẫu tương đối đã cho trong thư mục được biểu thị bằng đường dẫn này, mang lại tất cả các tệp phù hợp (bất kỳ loại nào):

>>> from pathlib import Path
4

Các mẫu giống như đối với

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
54, với việc bổ sung các ____ ____155, có nghĩa là thư mục này và tất cả các thư mục con, đệ quy. Nói cách khác, nó cho phép đệ quy:

>>> from pathlib import Path
5

Ghi chú

Sử dụng mẫu ____ ____155 trên các cây thư mục lớn có thể tiêu thụ một khoảng thời gian không phù hợp.

Tăng một sự kiện kiểm toán

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
57 với các đối số
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
58,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
59.auditing event
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
57 with arguments
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
58,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
59.

Đã thay đổi trong phiên bản 3.11: Chỉ trả lại các thư mục nếu mẫu kết thúc bằng bộ phân cách thành phần PathName (

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
60 hoặc
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
61).Return only directories if pattern ends with a pathname components separator (
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
60 or
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
61).

Path.group () ¶group()

Trả về tên của nhóm sở hữu tập tin.

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
62 được nâng lên nếu tệp Gid được tìm thấy trong cơ sở dữ liệu hệ thống.

Path.is_dir ()is_dir()

Trả về

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn trỏ đến một thư mục (hoặc liên kết tượng trưng chỉ vào một thư mục),
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu nó trỏ đến một loại tệp khác.

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 cũng được trả lại nếu đường dẫn không tồn tại hoặc là một liên kết symlink bị hỏng; Các lỗi khác (như lỗi cấp phép) được truyền.

Path.is_file () ¶is_file()

Trả về

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn trỏ đến một tệp thông thường (hoặc liên kết tượng trưng chỉ vào một tệp thông thường),
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu nó trỏ đến một loại tệp khác.

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 cũng được trả lại nếu đường dẫn không tồn tại hoặc là một liên kết symlink bị hỏng; Các lỗi khác (như lỗi cấp phép) được truyền.

Path.is_file () ¶is_mount()

Trả về

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn trỏ đến một tệp thông thường (hoặc liên kết tượng trưng chỉ vào một tệp thông thường),
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu nó trỏ đến một loại tệp khác.

Path.is_mount () ¶

Trả về
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn là điểm gắn kết: một điểm trong hệ thống tệp trong đó một hệ thống tệp khác đã được gắn kết. Trên POSIX, chức năng kiểm tra xem cha mẹ đường dẫn,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
70, có trên một thiết bị khác với đường dẫn hay liệu
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
70 và đường dẫn chỉ vào cùng một nút I trên cùng một thiết bị-điều này sẽ phát hiện các điểm gắn kết cho tất cả các biến thể Unix và POSIX. Không được triển khai trên Windows.
is_symlink()

Mới trong phiên bản 3.7.

Path.is_symlink () ¶

Trả về
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn trỏ đến một liên kết tượng trưng, ​​
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 khác.
is_socket()

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 cũng được trả lại nếu đường dẫn không tồn tại; Các lỗi khác (như lỗi cấp phép) được truyền.

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 cũng được trả lại nếu đường dẫn không tồn tại hoặc là một liên kết symlink bị hỏng; Các lỗi khác (như lỗi cấp phép) được truyền.

Path.is_file () ¶is_fifo()

Trả về

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn trỏ đến một tệp thông thường (hoặc liên kết tượng trưng chỉ vào một tệp thông thường),
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu nó trỏ đến một loại tệp khác.

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 cũng được trả lại nếu đường dẫn không tồn tại hoặc là một liên kết symlink bị hỏng; Các lỗi khác (như lỗi cấp phép) được truyền.

Path.is_file () ¶is_block_device()

Trả về

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn trỏ đến một tệp thông thường (hoặc liên kết tượng trưng chỉ vào một tệp thông thường),
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu nó trỏ đến một loại tệp khác.

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 cũng được trả lại nếu đường dẫn không tồn tại hoặc là một liên kết symlink bị hỏng; Các lỗi khác (như lỗi cấp phép) được truyền.

Path.is_file () ¶is_char_device()

Trả về

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn trỏ đến một tệp thông thường (hoặc liên kết tượng trưng chỉ vào một tệp thông thường),
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu nó trỏ đến một loại tệp khác.

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 cũng được trả lại nếu đường dẫn không tồn tại hoặc là một liên kết symlink bị hỏng; Các lỗi khác (như lỗi cấp phép) được truyền.

Path.is_file () ¶iterdir()

Trả về

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn trỏ đến một tệp thông thường (hoặc liên kết tượng trưng chỉ vào một tệp thông thường),
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu nó trỏ đến một loại tệp khác.

>>> from pathlib import Path
6

Path.is_mount () ¶

Trả về
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn là điểm gắn kết: một điểm trong hệ thống tệp trong đó một hệ thống tệp khác đã được gắn kết. Trên POSIX, chức năng kiểm tra xem cha mẹ đường dẫn,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
70, có trên một thiết bị khác với đường dẫn hay liệu
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
70 và đường dẫn chỉ vào cùng một nút I trên cùng một thiết bị-điều này sẽ phát hiện các điểm gắn kết cho tất cả các biến thể Unix và POSIX. Không được triển khai trên Windows.
lchmod(mode)

Mới trong phiên bản 3.7.

Path.is_symlink () ¶lstat()

Trả về

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn trỏ đến một liên kết tượng trưng, ​​
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 khác.

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 cũng được trả lại nếu đường dẫn không tồn tại; Các lỗi khác (như lỗi cấp phép) được truyền.
mkdir(mode=0o777, parents=False, exist_ok=False)

Path.is_socket () ¶

Trả về

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn trỏ đến ổ cắm UNIX (hoặc liên kết tượng trưng chỉ vào ổ cắm UNIX),
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu nó trỏ đến một loại tệp khác.

Path.is_fifo () ¶

Trả về

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17 Nếu đường dẫn trỏ đến FIFO (hoặc liên kết tượng trưng chỉ vào FIFO),
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18 nếu nó trỏ đến một loại tệp khác.

Nếu tồn tại_ok là đúng, các ngoại lệ

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
92 sẽ bị bỏ qua (hành vi tương tự như lệnh POSIX
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
93), nhưng chỉ khi thành phần đường dẫn cuối cùng không phải là một tệp không trực tiếp hiện có.

Đã thay đổi trong phiên bản 3.5: Tham số tồn tại_ok đã được thêm vào.The exist_ok parameter was added.

Path.open (mode = 'r', buffering = -1, mã hóa = none, error = none, newLine = none) ¶open(mode='r', buffering=- 1, encoding=None, errors=None, newline=None)

Mở tệp được trỏ bởi đường dẫn, giống như hàm

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
98 tích hợp không:

>>> from pathlib import Path
7

Path.owner () ¶owner()

Trả về tên của người dùng sở hữu tệp.

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
62 được nâng lên nếu tệp UID được tìm thấy trong cơ sở dữ liệu hệ thống.

Path.read_bytes () ¶read_bytes()

Trả về nội dung nhị phân của tệp pointed-to làm đối tượng byte:

>>> from pathlib import Path
8

Mới trong phiên bản 3.5.

Path.read_text (mã hóa = none, error = none) ¶read_text(encoding=None, errors=None)

Trả về nội dung được giải mã của tệp pointed-to dưới dạng chuỗi:

>>> from pathlib import Path
9

Các tập tin được mở và sau đó đóng. Các tham số tùy chọn có cùng ý nghĩa như trong

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
98.

Mới trong phiên bản 3.5.

Path.read_text (mã hóa = none, error = none) ¶readlink()

Trả về nội dung được giải mã của tệp pointed-to dưới dạng chuỗi:

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

Các tập tin được mở và sau đó đóng. Các tham số tùy chọn có cùng ý nghĩa như trong

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
98.

Path.ReadLink () ¶rename(target)

Trả về đường dẫn mà các điểm liên kết tượng trưng (được trả về bởi

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
01):

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

Mới trong phiên bản 3.9.

Path.rename (mục tiêu) ¶Added return value, return the new Path instance.

Đổi tên tệp hoặc thư mục này thành mục tiêu đã cho và trả về một thể hiện đường dẫn mới trỏ đến mục tiêu. Trên Unix, nếu Target tồn tại và là một tệp, nó sẽ được thay thế âm thầm nếu người dùng có quyền. Trên Windows, nếu Target tồn tại,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
92 sẽ được nâng lên. Mục tiêu có thể là một chuỗi hoặc đối tượng đường dẫn khác:
replace(target)

Đường dẫn mục tiêu có thể là tuyệt đối hoặc tương đối. Các đường dẫn tương đối được giải thích so với thư mục làm việc hiện tại, không phải thư mục của đối tượng đường dẫn.

Mới trong phiên bản 3.9.

Path.rename (mục tiêu) ¶Added return value, return the new Path instance.

Đổi tên tệp hoặc thư mục này thành mục tiêu đã cho và trả về một thể hiện đường dẫn mới trỏ đến mục tiêu. Trên Unix, nếu Target tồn tại và là một tệp, nó sẽ được thay thế âm thầm nếu người dùng có quyền. Trên Windows, nếu Target tồn tại,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
92 sẽ được nâng lên. Mục tiêu có thể là một chuỗi hoặc đối tượng đường dẫn khác:
absolute()

Đường dẫn mục tiêu có thể là tuyệt đối hoặc tương đối. Các đường dẫn tương đối được giải thích so với thư mục làm việc hiện tại, không phải thư mục của đối tượng đường dẫn.

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

Đã thay đổi trong phiên bản 3.8: Đã thêm giá trị trả về, trả về phiên bản đường dẫn mới.resolve(strict=False)

Path.replace (mục tiêu) ¶

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

Đổi tên tệp hoặc thư mục này thành mục tiêu đã cho và trả về một thể hiện đường dẫn mới trỏ đến mục tiêu. Nếu mục tiêu trỏ đến một tệp hiện có hoặc thư mục trống, nó sẽ được thay thế vô điều kiện.

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

Path.absolute () ¶

Làm cho đường dẫn tuyệt đối, mà không cần chuẩn hóa hoặc giải quyết các liên kết symlink. Trả về một đối tượng đường dẫn mới:The strict argument (pre-3.6 behavior is strict).

Path.resolve (nghiêm ngặt = false) ¶rglob(pattern)

Làm cho đường dẫn tuyệt đối, giải quyết bất kỳ liên kết symlink. Một đối tượng đường dẫn mới được trả về:

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

Các thành phần của ____ ____203 cũng bị loại bỏ (đây là phương pháp duy nhất để làm như vậy):auditing event

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
10 with arguments
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
58,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
59.

Nếu con đường không tồn tại và nghiêm ngặt là

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
17,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
94 sẽ được nâng lên. Nếu nghiêm ngặt là
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
18, đường dẫn được giải quyết càng xa càng tốt và bất kỳ phần còn lại nào cũng được thêm vào mà không kiểm tra xem nó có tồn tại hay không. Nếu một vòng lặp vô hạn gặp phải dọc theo đường phân giải,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
41 sẽ được nâng lên.Return only directories if pattern ends with a pathname components separator (
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
60 or
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
61).

Mới trong phiên bản 3.6: Đối số nghiêm ngặt (hành vi trước 3,6 là nghiêm ngặt).rmdir()

Path.rglob (mẫu) ¶

Điều này giống như gọi
from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
08 với
from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
09, được thêm vào trước mẫu tương đối đã cho:
samefile(other_path)

Tăng một sự kiện kiểm toán

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
10 với các đối số
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
58,
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
59.

Đã thay đổi trong phiên bản 3.11: Chỉ trả lại các thư mục nếu mẫu kết thúc bằng bộ phân cách thành phần PathName (

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
60 hoặc
    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
61).

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

Mới trong phiên bản 3.5.

Path.read_text (mã hóa = none, error = none) ¶symlink_to(target, target_is_directory=False)

Trả về nội dung được giải mã của tệp pointed-to dưới dạng chuỗi:

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

Các tập tin được mở và sau đó đóng. Các tham số tùy chọn có cùng ý nghĩa như trong

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
98.

Path.ReadLink () ¶

Trả về đường dẫn mà các điểm liên kết tượng trưng (được trả về bởi
from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
01):
hardlink_to(target)

Mới trong phiên bản 3.9.

Các tập tin được mở và sau đó đóng. Các tham số tùy chọn có cùng ý nghĩa như trong

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
98.

Path.ReadLink () ¶

Trả về đường dẫn mà các điểm liên kết tượng trưng (được trả về bởi

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
01):

Mới trong phiên bản 3.9.link_to(target)

Path.rename (mục tiêu) ¶

Đổi tên tệp hoặc thư mục này thành mục tiêu đã cho và trả về một thể hiện đường dẫn mới trỏ đến mục tiêu. Trên Unix, nếu Target tồn tại và là một tệp, nó sẽ được thay thế âm thầm nếu người dùng có quyền. Trên Windows, nếu Target tồn tại,

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
92 sẽ được nâng lên. Mục tiêu có thể là một chuỗi hoặc đối tượng đường dẫn khác:

Đường dẫn mục tiêu có thể là tuyệt đối hoặc tương đối. Các đường dẫn tương đối được giải thích so với thư mục làm việc hiện tại, không phải thư mục của đối tượng đường dẫn.

Đã thay đổi trong phiên bản 3.8: Đã thêm giá trị trả về, trả về phiên bản đường dẫn mới.

Path.replace (mục tiêu) ¶touch(mode=0o666, exist_ok=True)

Đổi tên tệp hoặc thư mục này thành mục tiêu đã cho và trả về một thể hiện đường dẫn mới trỏ đến mục tiêu. Nếu mục tiêu trỏ đến một tệp hiện có hoặc thư mục trống, nó sẽ được thay thế vô điều kiện.

Path.unlink (thiếu_ok = false) ¶unlink(missing_ok=False)

Xóa tệp này hoặc liên kết tượng trưng. Nếu đường dẫn chỉ vào một thư mục, hãy sử dụng

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
26 thay thế.

Nếu thiếu_ok là sai (mặc định),

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
94 sẽ được nâng lên nếu đường dẫn không tồn tại.

Nếu thiếu_ok là đúng, các ngoại lệ

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
94 sẽ bị bỏ qua (hành vi tương tự như lệnh POSIX
from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
29).

Đã thay đổi trong phiên bản 3.8: Tham số bị thiếu_OK đã được thêm vào.The missing_ok parameter was added.

Path.write_bytes (dữ liệu) ¶write_bytes(data)

Mở tệp được trỏ đến trong chế độ byte, ghi dữ liệu vào nó và đóng tệp:

>>> from pathlib import Path
8

Một tập tin hiện có cùng tên bị ghi đè.

Mới trong phiên bản 3.5.

Path.write_text (dữ liệu, mã hóa = none, error = none, newLine = none) ¶write_text(data, encoding=None, errors=None, newline=None)

Mở tệp được trỏ đến trong chế độ văn bản, ghi dữ liệu vào nó và đóng tệp:

>>> from pathlib import Path
9

Một tập tin hiện có cùng tên bị ghi đè. Các tham số tùy chọn có cùng ý nghĩa như trong

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
98.

Mới trong phiên bản 3.5.

Path.write_text (dữ liệu, mã hóa = none, error = none, newLine = none) ¶The newline parameter was added.

Mở tệp được trỏ đến trong chế độ văn bản, ghi dữ liệu vào nó và đóng tệp:

Một tập tin hiện có cùng tên bị ghi đè. Các tham số tùy chọn có cùng ý nghĩa như trong

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
98.

Thay đổi trong phiên bản 3.10: Tham số Newline đã được thêm vào.

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
37

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
38

Tương ứng với các công cụ trong mô -đun

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
31

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
40

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
11

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
46

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
89

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
44

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
45

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
46

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
45

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
48

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
49

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
50

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
51

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
52

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
26

Dưới đây là một bảng ánh xạ khác nhau

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
31 các chức năng cho tương ứng tương ứng của chúng ____ 101/________ 86.

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
56

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
38

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
58

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
59

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
60

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
39

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
31 và
>>> list(p.glob('**/*.py'))
[PosixPath('test_pathlib.py'), PosixPath('setup.py'),
 PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
 PosixPath('build/lib/pathlib.py')]
9

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
64

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
65

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
66

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
67

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
68

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
69

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
70

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
71

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
20

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
22

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
19

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
21

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
01

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
77

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
78

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
39 1

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
43

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
54,
from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
55

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
84

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
85

>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
3

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
87

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
88

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
89

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
90

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
91

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
15

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
93

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
94

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
62 và
from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
63

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
79 2

1

    ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
     in ()
             1 from pathlib import Path
             2 Desktop = Path('Desktop')
       ----> 3 SubDeskTop = Desktop+"/subdir"

     TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
90,
from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
82,
from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
83

2

from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
95 và
from pathlib import Path

Desktop = Path('Desktop')

# print(Desktop)
WindowsPath('Desktop')

# extend the path to include subdir
SubDeskTop = Desktop / "subdir"

# print(SubDeskTop)
WindowsPath('Desktop/subdir')

# passing an absolute path has different behavior
SubDeskTop = Path('Desktop') / '/subdir'

# print(SubDeskTop)
WindowsPath('/subdir')
96

Pathlib có tốt hơn hệ điều hành không?

Với Pathlib, bạn có thể thực hiện tất cả các tác vụ xử lý tệp cơ bản mà bạn đã làm trước đây và có một số tính năng khác không tồn tại trong mô -đun HĐH. Sự khác biệt chính là pathlib trực quan và dễ sử dụng hơn. Tất cả các phương thức xử lý tệp hữu ích thuộc về các đối tượng đường dẫn.Pathlib is more intuitive and easy to use. All the useful file handling methods belong to the Path objects.

Pathlib có hoạt động với URL không?

Không .PhathLib dành cho các đường dẫn hệ thống tập tin (nghĩa là, các đường dẫn đến các tệp trên máy tính của bạn), trong khi các đường dẫn S3 là URI. Đây là một cuộc thảo luận về việc thêm chức năng URI vào mô -đun Pathlib một ngày nào đó. pathlib is for filesystem paths (i.e., paths to files on your computer), while S3 paths are URIs. heres a discussion about adding URI functionality to the pathlib module some day.

Đường dẫn cwd () là gì?

Trong Pathlib, con đường.Hàm cwd () được sử dụng để có được thư mục làm việc hiện tại và / toán tử được sử dụng thay cho hệ điều hành.đường dẫn.Tham gia để kết hợp các phần của đường dẫn vào một đối tượng đường dẫn hợp chất.Các mẫu làm tổ trong hệ điều hành.used to get the current working directory and / operator is used in place of os. path. join to combine parts of the path into a compound path object. The function nesting pattern in the os.

Python pathlib hoạt động như thế nào?

Mô -đun Pathlib của Python làm cho nó rất dễ dàng và hiệu quả để xử lý các đường dẫn tệp.Hệ điều hành.Mô -đun đường dẫn cũng có thể được sử dụng để xử lý các hoạt động tên đường dẫn.Sự khác biệt là mô -đun đường dẫn tạo các chuỗi đại diện cho các đường dẫn tệp trong khi pathlib tạo ra một đối tượng đường dẫn.creates a path object.