blob: 83f7c836f0e71cf84a031ac7c7283a0772ddabe7 [file] [log] [blame]
Antoine Pitrou31119e42013-11-22 17:38:12 +01001
2:mod:`pathlib` --- Object-oriented filesystem paths
3===================================================
4
5.. module:: pathlib
6 :synopsis: Object-oriented filesystem paths
7
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04008.. versionadded:: 3.4
9
10**Source code:** :source:`Lib/pathlib.py`
11
Antoine Pitrou31119e42013-11-22 17:38:12 +010012.. index:: single: path; operations
13
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040014--------------
Antoine Pitrou31119e42013-11-22 17:38:12 +010015
Antoine Pitrou31119e42013-11-22 17:38:12 +010016This module offers classes representing filesystem paths with semantics
17appropriate for different operating systems. Path classes are divided
18between :ref:`pure paths <pure-paths>`, which provide purely computational
19operations without I/O, and :ref:`concrete paths <concrete-paths>`, which
20inherit from pure paths but also provide I/O operations.
21
Eli Benderskyb6e66eb2013-11-28 06:53:05 -080022.. image:: pathlib-inheritance.png
23 :align: center
24
25If you've never used this module before or just aren't sure which class is
26right for your task, :class:`Path` is most likely what you need. It instantiates
27a :ref:`concrete path <concrete-paths>` for the platform the code is running on.
28
29Pure paths are useful in some special cases; for example:
30
31#. If you want to manipulate Windows paths on a Unix machine (or vice versa).
32 You cannot instantiate a :class:`WindowsPath` when running on Unix, but you
33 can instantiate :class:`PureWindowsPath`.
34#. You want to make sure that your code only manipulates paths without actually
35 accessing the OS. In this case, instantiating one of the pure classes may be
36 useful since those simply don't have any OS-accessing operations.
Antoine Pitrou31119e42013-11-22 17:38:12 +010037
Antoine Pitrou31119e42013-11-22 17:38:12 +010038.. seealso::
39 :pep:`428`: The pathlib module -- object-oriented filesystem paths.
40
41.. seealso::
42 For low-level path manipulation on strings, you can also use the
43 :mod:`os.path` module.
44
45
46Basic use
47---------
48
49Importing the main class::
50
51 >>> from pathlib import Path
52
53Listing subdirectories::
54
55 >>> p = Path('.')
56 >>> [x for x in p.iterdir() if x.is_dir()]
57 [PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),
58 PosixPath('__pycache__'), PosixPath('build')]
59
60Listing Python source files in this directory tree::
61
62 >>> list(p.glob('**/*.py'))
63 [PosixPath('test_pathlib.py'), PosixPath('setup.py'),
64 PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
65 PosixPath('build/lib/pathlib.py')]
66
67Navigating inside a directory tree::
68
69 >>> p = Path('/etc')
70 >>> q = p / 'init.d' / 'reboot'
71 >>> q
72 PosixPath('/etc/init.d/reboot')
73 >>> q.resolve()
74 PosixPath('/etc/rc.d/init.d/halt')
75
76Querying path properties::
77
78 >>> q.exists()
79 True
80 >>> q.is_dir()
81 False
82
83Opening a file::
84
85 >>> with q.open() as f: f.readline()
86 ...
87 '#!/bin/bash\n'
88
89
90.. _pure-paths:
91
92Pure paths
93----------
94
95Pure path objects provide path-handling operations which don't actually
96access a filesystem. There are three ways to access these classes, which
97we also call *flavours*:
98
Eli Benderskyb6e66eb2013-11-28 06:53:05 -080099.. class:: PurePath(*pathsegments)
Antoine Pitrou31119e42013-11-22 17:38:12 +0100100
Eli Benderskyb6e66eb2013-11-28 06:53:05 -0800101 A generic class that represents the system's path flavour (instantiating
102 it creates either a :class:`PurePosixPath` or a :class:`PureWindowsPath`)::
103
104 >>> PurePath('setup.py') # Running on a Unix machine
105 PurePosixPath('setup.py')
106
Antoine Pitrou8ad751e2015-04-12 00:08:02 +0200107 Each element of *pathsegments* can be either a string representing a
Brett Cannon568be632016-06-10 12:20:49 -0700108 path segment, an object implementing the :class:`os.PathLike` interface
109 which returns a string, or another path object::
Eli Benderskyb6e66eb2013-11-28 06:53:05 -0800110
111 >>> PurePath('foo', 'some/path', 'bar')
112 PurePosixPath('foo/some/path/bar')
113 >>> PurePath(Path('foo'), Path('bar'))
114 PurePosixPath('foo/bar')
115
116 When *pathsegments* is empty, the current directory is assumed::
117
118 >>> PurePath()
119 PurePosixPath('.')
120
121 When several absolute paths are given, the last is taken as an anchor
122 (mimicking :func:`os.path.join`'s behaviour)::
123
124 >>> PurePath('/etc', '/usr', 'lib64')
125 PurePosixPath('/usr/lib64')
126 >>> PureWindowsPath('c:/Windows', 'd:bar')
127 PureWindowsPath('d:bar')
128
129 However, in a Windows path, changing the local root doesn't discard the
130 previous drive setting::
131
132 >>> PureWindowsPath('c:/Windows', '/Program Files')
133 PureWindowsPath('c:/Program Files')
134
135 Spurious slashes and single dots are collapsed, but double dots (``'..'``)
136 are not, since this would change the meaning of a path in the face of
137 symbolic links::
138
139 >>> PurePath('foo//bar')
140 PurePosixPath('foo/bar')
141 >>> PurePath('foo/./bar')
142 PurePosixPath('foo/bar')
143 >>> PurePath('foo/../bar')
144 PurePosixPath('foo/../bar')
145
146 (a naïve approach would make ``PurePosixPath('foo/../bar')`` equivalent
147 to ``PurePosixPath('bar')``, which is wrong if ``foo`` is a symbolic link
148 to another directory)
149
Brett Cannon568be632016-06-10 12:20:49 -0700150 Pure path objects implement the :class:`os.PathLike` interface, allowing them
151 to be used anywhere the interface is accepted.
152
153 .. versionchanged:: 3.6
154 Added support for the :class:`os.PathLike` interface.
155
Eli Benderskyb6e66eb2013-11-28 06:53:05 -0800156.. class:: PurePosixPath(*pathsegments)
Antoine Pitrou31119e42013-11-22 17:38:12 +0100157
158 A subclass of :class:`PurePath`, this path flavour represents non-Windows
159 filesystem paths::
160
161 >>> PurePosixPath('/etc')
162 PurePosixPath('/etc')
163
Eli Benderskyb6e66eb2013-11-28 06:53:05 -0800164 *pathsegments* is specified similarly to :class:`PurePath`.
165
166.. class:: PureWindowsPath(*pathsegments)
Antoine Pitrou31119e42013-11-22 17:38:12 +0100167
168 A subclass of :class:`PurePath`, this path flavour represents Windows
169 filesystem paths::
170
171 >>> PureWindowsPath('c:/Program Files/')
172 PureWindowsPath('c:/Program Files')
173
Eli Benderskyb6e66eb2013-11-28 06:53:05 -0800174 *pathsegments* is specified similarly to :class:`PurePath`.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100175
176Regardless of the system you're running on, you can instantiate all of
177these classes, since they don't provide any operation that does system calls.
178
179
Antoine Pitrou31119e42013-11-22 17:38:12 +0100180General properties
181^^^^^^^^^^^^^^^^^^
182
183Paths are immutable and hashable. Paths of a same flavour are comparable
184and orderable. These properties respect the flavour's case-folding
185semantics::
186
187 >>> PurePosixPath('foo') == PurePosixPath('FOO')
188 False
189 >>> PureWindowsPath('foo') == PureWindowsPath('FOO')
190 True
191 >>> PureWindowsPath('FOO') in { PureWindowsPath('foo') }
192 True
193 >>> PureWindowsPath('C:') < PureWindowsPath('d:')
194 True
195
196Paths of a different flavour compare unequal and cannot be ordered::
197
198 >>> PureWindowsPath('foo') == PurePosixPath('foo')
199 False
200 >>> PureWindowsPath('foo') < PurePosixPath('foo')
201 Traceback (most recent call last):
202 File "<stdin>", line 1, in <module>
Victor Stinner91108f02015-10-14 18:25:31 +0200203 TypeError: '<' not supported between instances of 'PureWindowsPath' and 'PurePosixPath'
Antoine Pitrou31119e42013-11-22 17:38:12 +0100204
205
206Operators
207^^^^^^^^^
208
209The slash operator helps create child paths, similarly to :func:`os.path.join`::
210
211 >>> p = PurePath('/etc')
212 >>> p
213 PurePosixPath('/etc')
214 >>> p / 'init.d' / 'apache2'
215 PurePosixPath('/etc/init.d/apache2')
216 >>> q = PurePath('bin')
217 >>> '/usr' / q
218 PurePosixPath('/usr/bin')
219
Brett Cannon568be632016-06-10 12:20:49 -0700220A path object can be used anywhere an object implementing :class:`os.PathLike`
221is accepted::
222
223 >>> import os
224 >>> p = PurePath('/etc')
225 >>> os.fspath(p)
226 '/etc'
227
Antoine Pitrou31119e42013-11-22 17:38:12 +0100228The string representation of a path is the raw filesystem path itself
229(in native form, e.g. with backslashes under Windows), which you can
230pass to any function taking a file path as a string::
231
232 >>> p = PurePath('/etc')
233 >>> str(p)
234 '/etc'
235 >>> p = PureWindowsPath('c:/Program Files')
236 >>> str(p)
237 'c:\\Program Files'
238
239Similarly, calling :class:`bytes` on a path gives the raw filesystem path as a
240bytes object, as encoded by :func:`os.fsencode`::
241
242 >>> bytes(p)
243 b'/etc'
244
245.. note::
246 Calling :class:`bytes` is only recommended under Unix. Under Windows,
247 the unicode form is the canonical representation of filesystem paths.
248
249
250Accessing individual parts
251^^^^^^^^^^^^^^^^^^^^^^^^^^
252
253To access the individual "parts" (components) of a path, use the following
254property:
255
256.. data:: PurePath.parts
257
258 A tuple giving access to the path's various components::
259
260 >>> p = PurePath('/usr/bin/python3')
261 >>> p.parts
262 ('/', 'usr', 'bin', 'python3')
263
264 >>> p = PureWindowsPath('c:/Program Files/PSF')
265 >>> p.parts
266 ('c:\\', 'Program Files', 'PSF')
267
268 (note how the drive and local root are regrouped in a single part)
269
270
271Methods and properties
272^^^^^^^^^^^^^^^^^^^^^^
273
Marco Buttu7b2491a2017-04-13 16:17:59 +0200274.. testsetup::
275
Hai Shi82642a02019-08-13 14:54:02 -0500276 from pathlib import PurePath, PurePosixPath, PureWindowsPath
Marco Buttu7b2491a2017-04-13 16:17:59 +0200277
Andrew Kuchling7a4e2d12013-11-22 15:45:02 -0500278Pure paths provide the following methods and properties:
Antoine Pitrou31119e42013-11-22 17:38:12 +0100279
280.. data:: PurePath.drive
281
282 A string representing the drive letter or name, if any::
283
284 >>> PureWindowsPath('c:/Program Files/').drive
285 'c:'
286 >>> PureWindowsPath('/Program Files/').drive
287 ''
288 >>> PurePosixPath('/etc').drive
289 ''
290
291 UNC shares are also considered drives::
292
293 >>> PureWindowsPath('//host/share/foo.txt').drive
294 '\\\\host\\share'
295
296.. data:: PurePath.root
297
298 A string representing the (local or global) root, if any::
299
300 >>> PureWindowsPath('c:/Program Files/').root
301 '\\'
302 >>> PureWindowsPath('c:Program Files/').root
303 ''
304 >>> PurePosixPath('/etc').root
305 '/'
306
307 UNC shares always have a root::
308
309 >>> PureWindowsPath('//host/share').root
310 '\\'
311
312.. data:: PurePath.anchor
313
314 The concatenation of the drive and root::
315
316 >>> PureWindowsPath('c:/Program Files/').anchor
317 'c:\\'
318 >>> PureWindowsPath('c:Program Files/').anchor
319 'c:'
320 >>> PurePosixPath('/etc').anchor
321 '/'
322 >>> PureWindowsPath('//host/share').anchor
323 '\\\\host\\share\\'
324
325
326.. data:: PurePath.parents
327
328 An immutable sequence providing access to the logical ancestors of
329 the path::
330
331 >>> p = PureWindowsPath('c:/foo/bar/setup.py')
332 >>> p.parents[0]
333 PureWindowsPath('c:/foo/bar')
334 >>> p.parents[1]
335 PureWindowsPath('c:/foo')
336 >>> p.parents[2]
337 PureWindowsPath('c:/')
338
339
340.. data:: PurePath.parent
341
342 The logical parent of the path::
343
344 >>> p = PurePosixPath('/a/b/c/d')
345 >>> p.parent
346 PurePosixPath('/a/b/c')
347
348 You cannot go past an anchor, or empty path::
349
350 >>> p = PurePosixPath('/')
351 >>> p.parent
352 PurePosixPath('/')
353 >>> p = PurePosixPath('.')
354 >>> p.parent
355 PurePosixPath('.')
356
357 .. note::
358 This is a purely lexical operation, hence the following behaviour::
359
360 >>> p = PurePosixPath('foo/..')
361 >>> p.parent
362 PurePosixPath('foo')
363
364 If you want to walk an arbitrary filesystem path upwards, it is
365 recommended to first call :meth:`Path.resolve` so as to resolve
366 symlinks and eliminate `".."` components.
367
368
369.. data:: PurePath.name
370
371 A string representing the final path component, excluding the drive and
372 root, if any::
373
374 >>> PurePosixPath('my/library/setup.py').name
375 'setup.py'
376
377 UNC drive names are not considered::
378
379 >>> PureWindowsPath('//some/share/setup.py').name
380 'setup.py'
381 >>> PureWindowsPath('//some/share').name
382 ''
383
384
385.. data:: PurePath.suffix
386
387 The file extension of the final component, if any::
388
389 >>> PurePosixPath('my/library/setup.py').suffix
390 '.py'
391 >>> PurePosixPath('my/library.tar.gz').suffix
392 '.gz'
393 >>> PurePosixPath('my/library').suffix
394 ''
395
396
397.. data:: PurePath.suffixes
398
399 A list of the path's file extensions::
400
401 >>> PurePosixPath('my/library.tar.gar').suffixes
402 ['.tar', '.gar']
403 >>> PurePosixPath('my/library.tar.gz').suffixes
404 ['.tar', '.gz']
405 >>> PurePosixPath('my/library').suffixes
406 []
407
408
409.. data:: PurePath.stem
410
411 The final path component, without its suffix::
412
413 >>> PurePosixPath('my/library.tar.gz').stem
414 'library.tar'
415 >>> PurePosixPath('my/library.tar').stem
416 'library'
417 >>> PurePosixPath('my/library').stem
418 'library'
419
420
421.. method:: PurePath.as_posix()
422
423 Return a string representation of the path with forward slashes (``/``)::
424
425 >>> p = PureWindowsPath('c:\\windows')
426 >>> str(p)
427 'c:\\windows'
428 >>> p.as_posix()
429 'c:/windows'
430
431
432.. method:: PurePath.as_uri()
433
434 Represent the path as a ``file`` URI. :exc:`ValueError` is raised if
435 the path isn't absolute.
436
437 >>> p = PurePosixPath('/etc/passwd')
438 >>> p.as_uri()
439 'file:///etc/passwd'
440 >>> p = PureWindowsPath('c:/Windows')
441 >>> p.as_uri()
442 'file:///c:/Windows'
443
444
445.. method:: PurePath.is_absolute()
446
447 Return whether the path is absolute or not. A path is considered absolute
448 if it has both a root and (if the flavour allows) a drive::
449
450 >>> PurePosixPath('/a/b').is_absolute()
451 True
452 >>> PurePosixPath('a/b').is_absolute()
453 False
454
455 >>> PureWindowsPath('c:/a/b').is_absolute()
456 True
457 >>> PureWindowsPath('/a/b').is_absolute()
458 False
459 >>> PureWindowsPath('c:').is_absolute()
460 False
461 >>> PureWindowsPath('//some/share').is_absolute()
462 True
463
464
Hai Shi82642a02019-08-13 14:54:02 -0500465.. method:: PurePath.is_relative_to(*other)
466
467 Return whether or not this path is relative to the *other* path.
468
469 >>> p = PurePath('/etc/passwd')
470 >>> p.is_relative_to('/etc')
471 True
472 >>> p.is_relative_to('/usr')
473 False
474
475 .. versionadded:: 3.9
476
477
Antoine Pitrou31119e42013-11-22 17:38:12 +0100478.. method:: PurePath.is_reserved()
479
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200480 With :class:`PureWindowsPath`, return ``True`` if the path is considered
481 reserved under Windows, ``False`` otherwise. With :class:`PurePosixPath`,
482 ``False`` is always returned.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100483
484 >>> PureWindowsPath('nul').is_reserved()
485 True
486 >>> PurePosixPath('nul').is_reserved()
487 False
488
489 File system calls on reserved paths can fail mysteriously or have
490 unintended effects.
491
492
493.. method:: PurePath.joinpath(*other)
494
Andrew Kuchling7a4e2d12013-11-22 15:45:02 -0500495 Calling this method is equivalent to combining the path with each of
Antoine Pitrou31119e42013-11-22 17:38:12 +0100496 the *other* arguments in turn::
497
498 >>> PurePosixPath('/etc').joinpath('passwd')
499 PurePosixPath('/etc/passwd')
500 >>> PurePosixPath('/etc').joinpath(PurePosixPath('passwd'))
501 PurePosixPath('/etc/passwd')
502 >>> PurePosixPath('/etc').joinpath('init.d', 'apache2')
503 PurePosixPath('/etc/init.d/apache2')
504 >>> PureWindowsPath('c:').joinpath('/Program Files')
505 PureWindowsPath('c:/Program Files')
506
507
508.. method:: PurePath.match(pattern)
509
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200510 Match this path against the provided glob-style pattern. Return ``True``
511 if matching is successful, ``False`` otherwise.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100512
513 If *pattern* is relative, the path can be either relative or absolute,
514 and matching is done from the right::
515
516 >>> PurePath('a/b.py').match('*.py')
517 True
518 >>> PurePath('/a/b/c.py').match('b/*.py')
519 True
520 >>> PurePath('/a/b/c.py').match('a/*.py')
521 False
522
523 If *pattern* is absolute, the path must be absolute, and the whole path
524 must match::
525
526 >>> PurePath('/a.py').match('/*.py')
527 True
528 >>> PurePath('a/b.py').match('/*.py')
529 False
530
Tim Loc12375a2020-04-19 05:43:11 -0400531 As with other methods, case-sensitivity follows platform defaults::
Antoine Pitrou31119e42013-11-22 17:38:12 +0100532
Tim Loc12375a2020-04-19 05:43:11 -0400533 >>> PurePosixPath('b.py').match('*.PY')
534 False
Antoine Pitrou31119e42013-11-22 17:38:12 +0100535 >>> PureWindowsPath('b.py').match('*.PY')
536 True
537
538
539.. method:: PurePath.relative_to(*other)
540
541 Compute a version of this path relative to the path represented by
542 *other*. If it's impossible, ValueError is raised::
543
544 >>> p = PurePosixPath('/etc/passwd')
545 >>> p.relative_to('/')
546 PurePosixPath('etc/passwd')
547 >>> p.relative_to('/etc')
548 PurePosixPath('passwd')
549 >>> p.relative_to('/usr')
550 Traceback (most recent call last):
551 File "<stdin>", line 1, in <module>
552 File "pathlib.py", line 694, in relative_to
553 .format(str(self), str(formatted)))
554 ValueError: '/etc/passwd' does not start with '/usr'
555
556
Antoine Pitrouef851192014-02-25 20:33:02 +0100557.. method:: PurePath.with_name(name)
558
559 Return a new path with the :attr:`name` changed. If the original path
560 doesn't have a name, ValueError is raised::
561
562 >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
563 >>> p.with_name('setup.py')
564 PureWindowsPath('c:/Downloads/setup.py')
565 >>> p = PureWindowsPath('c:/')
566 >>> p.with_name('setup.py')
567 Traceback (most recent call last):
568 File "<stdin>", line 1, in <module>
569 File "/home/antoine/cpython/default/Lib/pathlib.py", line 751, in with_name
570 raise ValueError("%r has an empty name" % (self,))
571 ValueError: PureWindowsPath('c:/') has an empty name
572
573
Tim Hoffmann8aea4b32020-04-19 17:29:49 +0200574.. method:: PurePath.with_stem(stem)
575
576 Return a new path with the :attr:`stem` changed. If the original path
577 doesn't have a name, ValueError is raised::
578
579 >>> p = PureWindowsPath('c:/Downloads/draft.txt')
580 >>> p.with_stem('final')
581 PureWindowsPath('c:/Downloads/final.txt')
582 >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
583 >>> p.with_stem('lib')
584 PureWindowsPath('c:/Downloads/lib.gz')
585 >>> p = PureWindowsPath('c:/')
586 >>> p.with_stem('')
587 Traceback (most recent call last):
588 File "<stdin>", line 1, in <module>
589 File "/home/antoine/cpython/default/Lib/pathlib.py", line 861, in with_stem
590 return self.with_name(stem + self.suffix)
591 File "/home/antoine/cpython/default/Lib/pathlib.py", line 851, in with_name
592 raise ValueError("%r has an empty name" % (self,))
593 ValueError: PureWindowsPath('c:/') has an empty name
594
595 .. versionadded:: 3.9
596
597
Antoine Pitrouef851192014-02-25 20:33:02 +0100598.. method:: PurePath.with_suffix(suffix)
599
600 Return a new path with the :attr:`suffix` changed. If the original path
Stefan Otte46dc4e32018-08-03 22:49:42 +0200601 doesn't have a suffix, the new *suffix* is appended instead. If the
602 *suffix* is an empty string, the original suffix is removed::
Antoine Pitrouef851192014-02-25 20:33:02 +0100603
604 >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
605 >>> p.with_suffix('.bz2')
606 PureWindowsPath('c:/Downloads/pathlib.tar.bz2')
607 >>> p = PureWindowsPath('README')
608 >>> p.with_suffix('.txt')
609 PureWindowsPath('README.txt')
Stefan Otte46dc4e32018-08-03 22:49:42 +0200610 >>> p = PureWindowsPath('README.txt')
611 >>> p.with_suffix('')
612 PureWindowsPath('README')
Antoine Pitrouef851192014-02-25 20:33:02 +0100613
614
Antoine Pitrou31119e42013-11-22 17:38:12 +0100615.. _concrete-paths:
616
617
618Concrete paths
619--------------
620
621Concrete paths are subclasses of the pure path classes. In addition to
622operations provided by the latter, they also provide methods to do system
623calls on path objects. There are three ways to instantiate concrete paths:
624
Eli Benderskyb6e66eb2013-11-28 06:53:05 -0800625.. class:: Path(*pathsegments)
Antoine Pitrou31119e42013-11-22 17:38:12 +0100626
627 A subclass of :class:`PurePath`, this class represents concrete paths of
628 the system's path flavour (instantiating it creates either a
629 :class:`PosixPath` or a :class:`WindowsPath`)::
630
631 >>> Path('setup.py')
632 PosixPath('setup.py')
633
Eli Benderskyb6e66eb2013-11-28 06:53:05 -0800634 *pathsegments* is specified similarly to :class:`PurePath`.
635
636.. class:: PosixPath(*pathsegments)
637
638 A subclass of :class:`Path` and :class:`PurePosixPath`, this class
639 represents concrete non-Windows filesystem paths::
640
641 >>> PosixPath('/etc')
642 PosixPath('/etc')
643
644 *pathsegments* is specified similarly to :class:`PurePath`.
645
646.. class:: WindowsPath(*pathsegments)
647
648 A subclass of :class:`Path` and :class:`PureWindowsPath`, this class
649 represents concrete Windows filesystem paths::
650
651 >>> WindowsPath('c:/Program Files/')
652 WindowsPath('c:/Program Files')
653
654 *pathsegments* is specified similarly to :class:`PurePath`.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100655
656You can only instantiate the class flavour that corresponds to your system
657(allowing system calls on non-compatible path flavours could lead to
658bugs or failures in your application)::
659
660 >>> import os
661 >>> os.name
662 'posix'
663 >>> Path('setup.py')
664 PosixPath('setup.py')
665 >>> PosixPath('setup.py')
666 PosixPath('setup.py')
667 >>> WindowsPath('setup.py')
668 Traceback (most recent call last):
669 File "<stdin>", line 1, in <module>
670 File "pathlib.py", line 798, in __new__
671 % (cls.__name__,))
672 NotImplementedError: cannot instantiate 'WindowsPath' on your system
673
674
675Methods
676^^^^^^^
677
678Concrete paths provide the following methods in addition to pure paths
679methods. Many of these methods can raise an :exc:`OSError` if a system
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300680call fails (for example because the path doesn't exist).
681
682.. versionchanged:: 3.8
683
684 :meth:`~Path.exists()`, :meth:`~Path.is_dir()`, :meth:`~Path.is_file()`,
685 :meth:`~Path.is_mount()`, :meth:`~Path.is_symlink()`,
686 :meth:`~Path.is_block_device()`, :meth:`~Path.is_char_device()`,
687 :meth:`~Path.is_fifo()`, :meth:`~Path.is_socket()` now return ``False``
688 instead of raising an exception for paths that contain characters
689 unrepresentable at the OS level.
690
Antoine Pitrou31119e42013-11-22 17:38:12 +0100691
692.. classmethod:: Path.cwd()
693
694 Return a new path object representing the current directory (as returned
695 by :func:`os.getcwd`)::
696
697 >>> Path.cwd()
698 PosixPath('/home/antoine/pathlib')
699
700
Antoine Pitrou17cba7d2015-01-12 21:03:41 +0100701.. classmethod:: Path.home()
702
703 Return a new path object representing the user's home directory (as
704 returned by :func:`os.path.expanduser` with ``~`` construct)::
705
706 >>> Path.home()
707 PosixPath('/home/antoine')
708
709 .. versionadded:: 3.5
710
711
Antoine Pitrou31119e42013-11-22 17:38:12 +0100712.. method:: Path.stat()
713
Brett Cannon67152d02020-03-04 14:51:50 -0800714 Return a :class:`os.stat_result` object containing information about this path, like :func:`os.stat`.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100715 The result is looked up at each call to this method.
716
Marco Buttu7b2491a2017-04-13 16:17:59 +0200717 ::
718
Antoine Pitrou31119e42013-11-22 17:38:12 +0100719 >>> p = Path('setup.py')
720 >>> p.stat().st_size
721 956
722 >>> p.stat().st_mtime
723 1327883547.852554
724
725
726.. method:: Path.chmod(mode)
727
728 Change the file mode and permissions, like :func:`os.chmod`::
729
730 >>> p = Path('setup.py')
731 >>> p.stat().st_mode
732 33277
733 >>> p.chmod(0o444)
734 >>> p.stat().st_mode
735 33060
736
737
738.. method:: Path.exists()
739
740 Whether the path points to an existing file or directory::
741
742 >>> Path('.').exists()
743 True
744 >>> Path('setup.py').exists()
745 True
746 >>> Path('/etc').exists()
747 True
748 >>> Path('nonexistentfile').exists()
749 False
750
751 .. note::
752 If the path points to a symlink, :meth:`exists` returns whether the
753 symlink *points to* an existing file or directory.
754
755
Antoine Pitrou8477ed62014-12-30 20:54:45 +0100756.. method:: Path.expanduser()
757
758 Return a new path with expanded ``~`` and ``~user`` constructs,
759 as returned by :meth:`os.path.expanduser`::
760
761 >>> p = PosixPath('~/films/Monty Python')
762 >>> p.expanduser()
763 PosixPath('/home/eric/films/Monty Python')
764
765 .. versionadded:: 3.5
766
767
Antoine Pitrou31119e42013-11-22 17:38:12 +0100768.. method:: Path.glob(pattern)
769
Eivind Teig537b6ca2019-02-11 11:47:09 +0100770 Glob the given relative *pattern* in the directory represented by this path,
Antoine Pitrou31119e42013-11-22 17:38:12 +0100771 yielding all matching files (of any kind)::
772
773 >>> sorted(Path('.').glob('*.py'))
774 [PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')]
775 >>> sorted(Path('.').glob('*/*.py'))
776 [PosixPath('docs/conf.py')]
777
778 The "``**``" pattern means "this directory and all subdirectories,
779 recursively". In other words, it enables recursive globbing::
780
781 >>> sorted(Path('.').glob('**/*.py'))
782 [PosixPath('build/lib/pathlib.py'),
783 PosixPath('docs/conf.py'),
784 PosixPath('pathlib.py'),
785 PosixPath('setup.py'),
786 PosixPath('test_pathlib.py')]
787
788 .. note::
789 Using the "``**``" pattern in large directory trees may consume
790 an inordinate amount of time.
791
Serhiy Storchakadb283b32020-03-08 14:31:47 +0200792 .. audit-event:: pathlib.Path.glob self,pattern pathlib.Path.glob
793
Antoine Pitrou31119e42013-11-22 17:38:12 +0100794
795.. method:: Path.group()
796
Ned Deilyc0341562013-11-27 14:42:55 -0800797 Return the name of the group owning the file. :exc:`KeyError` is raised
Antoine Pitrou31119e42013-11-22 17:38:12 +0100798 if the file's gid isn't found in the system database.
799
800
801.. method:: Path.is_dir()
802
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200803 Return ``True`` if the path points to a directory (or a symbolic link
804 pointing to a directory), ``False`` if it points to another kind of file.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100805
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200806 ``False`` is also returned if the path doesn't exist or is a broken symlink;
Antoine Pitrou31119e42013-11-22 17:38:12 +0100807 other errors (such as permission errors) are propagated.
808
809
810.. method:: Path.is_file()
811
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200812 Return ``True`` if the path points to a regular file (or a symbolic link
813 pointing to a regular file), ``False`` if it points to another kind of file.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100814
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200815 ``False`` is also returned if the path doesn't exist or is a broken symlink;
Antoine Pitrou31119e42013-11-22 17:38:12 +0100816 other errors (such as permission errors) are propagated.
817
818
Łukasz Langa47320a62017-08-01 16:47:50 -0700819.. method:: Path.is_mount()
820
821 Return ``True`` if the path is a :dfn:`mount point`: a point in a
822 file system where a different file system has been mounted. On POSIX, the
823 function checks whether *path*'s parent, :file:`path/..`, is on a different
824 device than *path*, or whether :file:`path/..` and *path* point to the same
825 i-node on the same device --- this should detect mount points for all Unix
826 and POSIX variants. Not implemented on Windows.
827
828 .. versionadded:: 3.7
829
830
Antoine Pitrou31119e42013-11-22 17:38:12 +0100831.. method:: Path.is_symlink()
832
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200833 Return ``True`` if the path points to a symbolic link, ``False`` otherwise.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100834
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200835 ``False`` is also returned if the path doesn't exist; other errors (such
Antoine Pitrou31119e42013-11-22 17:38:12 +0100836 as permission errors) are propagated.
837
838
839.. method:: Path.is_socket()
840
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200841 Return ``True`` if the path points to a Unix socket (or a symbolic link
842 pointing to a Unix socket), ``False`` if it points to another kind of file.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100843
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200844 ``False`` is also returned if the path doesn't exist or is a broken symlink;
Antoine Pitrou31119e42013-11-22 17:38:12 +0100845 other errors (such as permission errors) are propagated.
846
847
848.. method:: Path.is_fifo()
849
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200850 Return ``True`` if the path points to a FIFO (or a symbolic link
851 pointing to a FIFO), ``False`` if it points to another kind of file.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100852
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200853 ``False`` is also returned if the path doesn't exist or is a broken symlink;
Antoine Pitrou31119e42013-11-22 17:38:12 +0100854 other errors (such as permission errors) are propagated.
855
856
857.. method:: Path.is_block_device()
858
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200859 Return ``True`` if the path points to a block device (or a symbolic link
860 pointing to a block device), ``False`` if it points to another kind of file.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100861
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200862 ``False`` is also returned if the path doesn't exist or is a broken symlink;
Antoine Pitrou31119e42013-11-22 17:38:12 +0100863 other errors (such as permission errors) are propagated.
864
865
866.. method:: Path.is_char_device()
867
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200868 Return ``True`` if the path points to a character device (or a symbolic link
869 pointing to a character device), ``False`` if it points to another kind of file.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100870
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200871 ``False`` is also returned if the path doesn't exist or is a broken symlink;
Antoine Pitrou31119e42013-11-22 17:38:12 +0100872 other errors (such as permission errors) are propagated.
873
874
875.. method:: Path.iterdir()
876
877 When the path points to a directory, yield path objects of the directory
878 contents::
879
880 >>> p = Path('docs')
881 >>> for child in p.iterdir(): child
882 ...
883 PosixPath('docs/conf.py')
884 PosixPath('docs/_templates')
885 PosixPath('docs/make.bat')
886 PosixPath('docs/index.rst')
887 PosixPath('docs/_build')
888 PosixPath('docs/_static')
889 PosixPath('docs/Makefile')
890
891.. method:: Path.lchmod(mode)
892
893 Like :meth:`Path.chmod` but, if the path points to a symbolic link, the
894 symbolic link's mode is changed rather than its target's.
895
896
897.. method:: Path.lstat()
898
899 Like :meth:`Path.stat` but, if the path points to a symbolic link, return
900 the symbolic link's information rather than its target's.
901
902
Barry Warsaw7c549c42014-08-05 11:28:12 -0400903.. method:: Path.mkdir(mode=0o777, parents=False, exist_ok=False)
Antoine Pitrou31119e42013-11-22 17:38:12 +0100904
905 Create a new directory at this given path. If *mode* is given, it is
906 combined with the process' ``umask`` value to determine the file mode
Antoine Pitrouf6abb702013-12-16 21:00:53 +0100907 and access flags. If the path already exists, :exc:`FileExistsError`
908 is raised.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100909
Serhiy Storchaka03cc5652013-11-26 21:37:12 +0200910 If *parents* is true, any missing parents of this path are created
Antoine Pitrou0048c982013-12-16 20:22:37 +0100911 as needed; they are created with the default permissions without taking
912 *mode* into account (mimicking the POSIX ``mkdir -p`` command).
913
914 If *parents* is false (the default), a missing parent raises
Antoine Pitrouf6abb702013-12-16 21:00:53 +0100915 :exc:`FileNotFoundError`.
Antoine Pitrou31119e42013-11-22 17:38:12 +0100916
Ned Deily11194f72016-10-15 15:12:03 -0400917 If *exist_ok* is false (the default), :exc:`FileExistsError` is
Barry Warsaw7c549c42014-08-05 11:28:12 -0400918 raised if the target directory already exists.
919
920 If *exist_ok* is true, :exc:`FileExistsError` exceptions will be
921 ignored (same behavior as the POSIX ``mkdir -p`` command), but only if the
922 last path component is not an existing non-directory file.
923
924 .. versionchanged:: 3.5
925 The *exist_ok* parameter was added.
926
Antoine Pitrou31119e42013-11-22 17:38:12 +0100927
928.. method:: Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)
929
930 Open the file pointed to by the path, like the built-in :func:`open`
931 function does::
932
933 >>> p = Path('setup.py')
934 >>> with p.open() as f:
935 ... f.readline()
936 ...
937 '#!/usr/bin/env python3\n'
938
939
940.. method:: Path.owner()
941
Ned Deilyc0341562013-11-27 14:42:55 -0800942 Return the name of the user owning the file. :exc:`KeyError` is raised
Antoine Pitrou31119e42013-11-22 17:38:12 +0100943 if the file's uid isn't found in the system database.
944
945
Georg Brandlea683982014-10-01 19:12:33 +0200946.. method:: Path.read_bytes()
947
948 Return the binary contents of the pointed-to file as a bytes object::
949
950 >>> p = Path('my_binary_file')
951 >>> p.write_bytes(b'Binary file contents')
952 20
953 >>> p.read_bytes()
954 b'Binary file contents'
955
956 .. versionadded:: 3.5
957
958
959.. method:: Path.read_text(encoding=None, errors=None)
960
961 Return the decoded contents of the pointed-to file as a string::
962
963 >>> p = Path('my_text_file')
964 >>> p.write_text('Text file contents')
965 18
966 >>> p.read_text()
967 'Text file contents'
968
Xtreak5b2657f2018-08-07 01:25:03 +0530969 The file is opened and then closed. The optional parameters have the same
970 meaning as in :func:`open`.
Georg Brandlea683982014-10-01 19:12:33 +0200971
972 .. versionadded:: 3.5
973
974
Girtsa01ba332019-10-23 14:18:40 -0700975.. method:: Path.readlink()
976
977 Return the path to which the symbolic link points (as returned by
978 :func:`os.readlink`)::
979
980 >>> p = Path('mylink')
981 >>> p.symlink_to('setup.py')
982 >>> p.readlink()
983 PosixPath('setup.py')
984
985 .. versionadded:: 3.9
986
987
Antoine Pitrou31119e42013-11-22 17:38:12 +0100988.. method:: Path.rename(target)
989
hui shang088a09a2019-09-11 21:26:49 +0800990 Rename this file or directory to the given *target*, and return a new Path
991 instance pointing to *target*. On Unix, if *target* exists and is a file,
992 it will be replaced silently if the user has permission. *target* can be
993 either a string or another path object::
Antoine Pitrou31119e42013-11-22 17:38:12 +0100994
995 >>> p = Path('foo')
996 >>> p.open('w').write('some text')
997 9
998 >>> target = Path('bar')
999 >>> p.rename(target)
hui shang088a09a2019-09-11 21:26:49 +08001000 PosixPath('bar')
Antoine Pitrou31119e42013-11-22 17:38:12 +01001001 >>> target.open().read()
1002 'some text'
1003
hui shang088a09a2019-09-11 21:26:49 +08001004 .. versionchanged:: 3.8
1005 Added return value, return the new Path instance.
1006
Antoine Pitrou31119e42013-11-22 17:38:12 +01001007
1008.. method:: Path.replace(target)
1009
hui shang088a09a2019-09-11 21:26:49 +08001010 Rename this file or directory to the given *target*, and return a new Path
1011 instance pointing to *target*. If *target* points to an existing file or
1012 directory, it will be unconditionally replaced.
1013
1014 .. versionchanged:: 3.8
1015 Added return value, return the new Path instance.
Antoine Pitrou31119e42013-11-22 17:38:12 +01001016
1017
Steve Dower98eb3602016-11-09 12:58:17 -08001018.. method:: Path.resolve(strict=False)
Antoine Pitrou31119e42013-11-22 17:38:12 +01001019
1020 Make the path absolute, resolving any symlinks. A new path object is
1021 returned::
1022
1023 >>> p = Path()
1024 >>> p
1025 PosixPath('.')
1026 >>> p.resolve()
1027 PosixPath('/home/antoine/pathlib')
1028
Berker Peksag5e3677d2016-10-01 01:06:52 +03001029 "``..``" components are also eliminated (this is the only method to do so)::
Antoine Pitrou31119e42013-11-22 17:38:12 +01001030
1031 >>> p = Path('docs/../setup.py')
1032 >>> p.resolve()
1033 PosixPath('/home/antoine/pathlib/setup.py')
1034
Steve Dower98eb3602016-11-09 12:58:17 -08001035 If the path doesn't exist and *strict* is ``True``, :exc:`FileNotFoundError`
1036 is raised. If *strict* is ``False``, the path is resolved as far as possible
1037 and any remainder is appended without checking whether it exists. If an
1038 infinite loop is encountered along the resolution path, :exc:`RuntimeError`
1039 is raised.
Antoine Pitrou31119e42013-11-22 17:38:12 +01001040
Steve Dower98eb3602016-11-09 12:58:17 -08001041 .. versionadded:: 3.6
Julien Palard1d4b1602019-05-08 17:01:11 +02001042 The *strict* argument (pre-3.6 behavior is strict).
Antoine Pitrou31119e42013-11-22 17:38:12 +01001043
1044.. method:: Path.rglob(pattern)
1045
Eivind Teig537b6ca2019-02-11 11:47:09 +01001046 This is like calling :func:`Path.glob` with "``**/``" added in front of the
1047 given relative *pattern*::
Antoine Pitrou31119e42013-11-22 17:38:12 +01001048
1049 >>> sorted(Path().rglob("*.py"))
1050 [PosixPath('build/lib/pathlib.py'),
1051 PosixPath('docs/conf.py'),
1052 PosixPath('pathlib.py'),
1053 PosixPath('setup.py'),
1054 PosixPath('test_pathlib.py')]
1055
Serhiy Storchakadb283b32020-03-08 14:31:47 +02001056 .. audit-event:: pathlib.Path.rglob self,pattern pathlib.Path.rglob
1057
Antoine Pitrou31119e42013-11-22 17:38:12 +01001058
1059.. method:: Path.rmdir()
1060
1061 Remove this directory. The directory must be empty.
1062
1063
Antoine Pitrou43e3d942014-05-13 10:50:15 +02001064.. method:: Path.samefile(other_path)
1065
1066 Return whether this path points to the same file as *other_path*, which
1067 can be either a Path object, or a string. The semantics are similar
1068 to :func:`os.path.samefile` and :func:`os.path.samestat`.
1069
1070 An :exc:`OSError` can be raised if either file cannot be accessed for some
1071 reason.
1072
Marco Buttu7b2491a2017-04-13 16:17:59 +02001073 ::
1074
Antoine Pitrou43e3d942014-05-13 10:50:15 +02001075 >>> p = Path('spam')
1076 >>> q = Path('eggs')
1077 >>> p.samefile(q)
1078 False
1079 >>> p.samefile('spam')
1080 True
1081
1082 .. versionadded:: 3.5
1083
1084
Antoine Pitrou31119e42013-11-22 17:38:12 +01001085.. method:: Path.symlink_to(target, target_is_directory=False)
1086
1087 Make this path a symbolic link to *target*. Under Windows,
Serhiy Storchaka03cc5652013-11-26 21:37:12 +02001088 *target_is_directory* must be true (default ``False``) if the link's target
Antoine Pitrou31119e42013-11-22 17:38:12 +01001089 is a directory. Under POSIX, *target_is_directory*'s value is ignored.
1090
Marco Buttu7b2491a2017-04-13 16:17:59 +02001091 ::
1092
Antoine Pitrou31119e42013-11-22 17:38:12 +01001093 >>> p = Path('mylink')
1094 >>> p.symlink_to('setup.py')
1095 >>> p.resolve()
1096 PosixPath('/home/antoine/pathlib/setup.py')
1097 >>> p.stat().st_size
1098 956
1099 >>> p.lstat().st_size
1100 8
1101
1102 .. note::
1103 The order of arguments (link, target) is the reverse
1104 of :func:`os.symlink`'s.
1105
1106
Zachary Ware7a26da52016-08-09 17:10:39 -05001107.. method:: Path.touch(mode=0o666, exist_ok=True)
Antoine Pitrou31119e42013-11-22 17:38:12 +01001108
1109 Create a file at this given path. If *mode* is given, it is combined
1110 with the process' ``umask`` value to determine the file mode and access
1111 flags. If the file already exists, the function succeeds if *exist_ok*
1112 is true (and its modification time is updated to the current time),
Antoine Pitrouf6abb702013-12-16 21:00:53 +01001113 otherwise :exc:`FileExistsError` is raised.
Antoine Pitrou31119e42013-11-22 17:38:12 +01001114
1115
‮zlohhcuB treboRd9e006b2019-05-16 00:02:11 +02001116.. method:: Path.unlink(missing_ok=False)
Antoine Pitrou31119e42013-11-22 17:38:12 +01001117
1118 Remove this file or symbolic link. If the path points to a directory,
1119 use :func:`Path.rmdir` instead.
Georg Brandlea683982014-10-01 19:12:33 +02001120
‮zlohhcuB treboRd9e006b2019-05-16 00:02:11 +02001121 If *missing_ok* is false (the default), :exc:`FileNotFoundError` is
1122 raised if the path does not exist.
1123
1124 If *missing_ok* is true, :exc:`FileNotFoundError` exceptions will be
1125 ignored (same behavior as the POSIX ``rm -f`` command).
1126
1127 .. versionchanged:: 3.8
1128 The *missing_ok* parameter was added.
1129
Georg Brandlea683982014-10-01 19:12:33 +02001130
Joannah Nanjekye6b5b0132019-05-04 11:27:10 -04001131.. method:: Path.link_to(target)
1132
1133 Create a hard link pointing to a path named *target*.
1134
1135 .. versionchanged:: 3.8
1136
1137
Georg Brandlea683982014-10-01 19:12:33 +02001138.. method:: Path.write_bytes(data)
1139
1140 Open the file pointed to in bytes mode, write *data* to it, and close the
1141 file::
1142
1143 >>> p = Path('my_binary_file')
1144 >>> p.write_bytes(b'Binary file contents')
1145 20
1146 >>> p.read_bytes()
1147 b'Binary file contents'
1148
1149 An existing file of the same name is overwritten.
1150
1151 .. versionadded:: 3.5
1152
1153
1154.. method:: Path.write_text(data, encoding=None, errors=None)
1155
1156 Open the file pointed to in text mode, write *data* to it, and close the
1157 file::
1158
1159 >>> p = Path('my_text_file')
1160 >>> p.write_text('Text file contents')
1161 18
1162 >>> p.read_text()
1163 'Text file contents'
1164
Lysandros Nikolaouaf636f42019-09-11 18:08:10 +03001165 An existing file of the same name is overwritten. The optional parameters
1166 have the same meaning as in :func:`open`.
1167
Georg Brandlea683982014-10-01 19:12:33 +02001168 .. versionadded:: 3.5
Jamiel Almeidaae8750b2017-06-02 11:36:02 -07001169
1170Correspondence to tools in the :mod:`os` module
1171-----------------------------------------------
1172
1173Below is a table mapping various :mod:`os` functions to their corresponding
1174:class:`PurePath`/:class:`Path` equivalent.
1175
1176.. note::
1177
1178 Although :func:`os.path.relpath` and :meth:`PurePath.relative_to` have some
1179 overlapping use-cases, their semantics differ enough to warrant not
1180 considering them equivalent.
1181
Xtreak6f9c55d2018-10-05 20:54:11 +05301182==================================== ==============================
1183os and os.path pathlib
1184==================================== ==============================
1185:func:`os.path.abspath` :meth:`Path.resolve`
1186:func:`os.chmod` :meth:`Path.chmod`
1187:func:`os.mkdir` :meth:`Path.mkdir`
Joannah Nanjekyef25fb6e2020-05-04 16:47:03 -03001188:func:`os.makedirs` :meth:`Path.mkdir`
Xtreak6f9c55d2018-10-05 20:54:11 +05301189:func:`os.rename` :meth:`Path.rename`
1190:func:`os.replace` :meth:`Path.replace`
1191:func:`os.rmdir` :meth:`Path.rmdir`
1192:func:`os.remove`, :func:`os.unlink` :meth:`Path.unlink`
1193:func:`os.getcwd` :func:`Path.cwd`
1194:func:`os.path.exists` :meth:`Path.exists`
1195:func:`os.path.expanduser` :meth:`Path.expanduser` and
1196 :meth:`Path.home`
1197:func:`os.path.isdir` :meth:`Path.is_dir`
1198:func:`os.path.isfile` :meth:`Path.is_file`
1199:func:`os.path.islink` :meth:`Path.is_symlink`
Girtsa01ba332019-10-23 14:18:40 -07001200:func:`os.readlink` :meth:`Path.readlink`
Xtreak6f9c55d2018-10-05 20:54:11 +05301201:func:`os.stat` :meth:`Path.stat`,
1202 :meth:`Path.owner`,
1203 :meth:`Path.group`
1204:func:`os.path.isabs` :meth:`PurePath.is_absolute`
1205:func:`os.path.join` :func:`PurePath.joinpath`
1206:func:`os.path.basename` :data:`PurePath.name`
1207:func:`os.path.dirname` :data:`PurePath.parent`
1208:func:`os.path.samefile` :meth:`Path.samefile`
1209:func:`os.path.splitext` :data:`PurePath.suffix`
1210==================================== ==============================