Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1 | |
| 2 | :mod:`pathlib` --- Object-oriented filesystem paths |
| 3 | =================================================== |
| 4 | |
| 5 | .. module:: pathlib |
| 6 | :synopsis: Object-oriented filesystem paths |
| 7 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 8 | .. versionadded:: 3.4 |
| 9 | |
| 10 | **Source code:** :source:`Lib/pathlib.py` |
| 11 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 12 | .. index:: single: path; operations |
| 13 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 14 | -------------- |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 15 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 16 | This module offers classes representing filesystem paths with semantics |
| 17 | appropriate for different operating systems. Path classes are divided |
| 18 | between :ref:`pure paths <pure-paths>`, which provide purely computational |
| 19 | operations without I/O, and :ref:`concrete paths <concrete-paths>`, which |
| 20 | inherit from pure paths but also provide I/O operations. |
| 21 | |
Eli Bendersky | b6e66eb | 2013-11-28 06:53:05 -0800 | [diff] [blame] | 22 | .. image:: pathlib-inheritance.png |
| 23 | :align: center |
| 24 | |
| 25 | If you've never used this module before or just aren't sure which class is |
| 26 | right for your task, :class:`Path` is most likely what you need. It instantiates |
| 27 | a :ref:`concrete path <concrete-paths>` for the platform the code is running on. |
| 28 | |
| 29 | Pure 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 Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 37 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 38 | .. 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 | |
| 46 | Basic use |
| 47 | --------- |
| 48 | |
| 49 | Importing the main class:: |
| 50 | |
| 51 | >>> from pathlib import Path |
| 52 | |
| 53 | Listing 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 | |
| 60 | Listing 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 | |
| 67 | Navigating 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 | |
| 76 | Querying path properties:: |
| 77 | |
| 78 | >>> q.exists() |
| 79 | True |
| 80 | >>> q.is_dir() |
| 81 | False |
| 82 | |
| 83 | Opening a file:: |
| 84 | |
| 85 | >>> with q.open() as f: f.readline() |
| 86 | ... |
| 87 | '#!/bin/bash\n' |
| 88 | |
| 89 | |
| 90 | .. _pure-paths: |
| 91 | |
| 92 | Pure paths |
| 93 | ---------- |
| 94 | |
| 95 | Pure path objects provide path-handling operations which don't actually |
| 96 | access a filesystem. There are three ways to access these classes, which |
| 97 | we also call *flavours*: |
| 98 | |
Eli Bendersky | b6e66eb | 2013-11-28 06:53:05 -0800 | [diff] [blame] | 99 | .. class:: PurePath(*pathsegments) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 100 | |
Eli Bendersky | b6e66eb | 2013-11-28 06:53:05 -0800 | [diff] [blame] | 101 | 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 Pitrou | 8ad751e | 2015-04-12 00:08:02 +0200 | [diff] [blame] | 107 | Each element of *pathsegments* can be either a string representing a |
Brett Cannon | 568be63 | 2016-06-10 12:20:49 -0700 | [diff] [blame] | 108 | path segment, an object implementing the :class:`os.PathLike` interface |
| 109 | which returns a string, or another path object:: |
Eli Bendersky | b6e66eb | 2013-11-28 06:53:05 -0800 | [diff] [blame] | 110 | |
| 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 Cannon | 568be63 | 2016-06-10 12:20:49 -0700 | [diff] [blame] | 150 | 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 Bendersky | b6e66eb | 2013-11-28 06:53:05 -0800 | [diff] [blame] | 156 | .. class:: PurePosixPath(*pathsegments) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 157 | |
| 158 | A subclass of :class:`PurePath`, this path flavour represents non-Windows |
| 159 | filesystem paths:: |
| 160 | |
| 161 | >>> PurePosixPath('/etc') |
| 162 | PurePosixPath('/etc') |
| 163 | |
Eli Bendersky | b6e66eb | 2013-11-28 06:53:05 -0800 | [diff] [blame] | 164 | *pathsegments* is specified similarly to :class:`PurePath`. |
| 165 | |
| 166 | .. class:: PureWindowsPath(*pathsegments) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 167 | |
| 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 Bendersky | b6e66eb | 2013-11-28 06:53:05 -0800 | [diff] [blame] | 174 | *pathsegments* is specified similarly to :class:`PurePath`. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 175 | |
| 176 | Regardless of the system you're running on, you can instantiate all of |
| 177 | these classes, since they don't provide any operation that does system calls. |
| 178 | |
| 179 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 180 | General properties |
| 181 | ^^^^^^^^^^^^^^^^^^ |
| 182 | |
| 183 | Paths are immutable and hashable. Paths of a same flavour are comparable |
| 184 | and orderable. These properties respect the flavour's case-folding |
| 185 | semantics:: |
| 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 | |
| 196 | Paths 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 Stinner | 91108f0 | 2015-10-14 18:25:31 +0200 | [diff] [blame] | 203 | TypeError: '<' not supported between instances of 'PureWindowsPath' and 'PurePosixPath' |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 204 | |
| 205 | |
| 206 | Operators |
| 207 | ^^^^^^^^^ |
| 208 | |
| 209 | The 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 Cannon | 568be63 | 2016-06-10 12:20:49 -0700 | [diff] [blame] | 220 | A path object can be used anywhere an object implementing :class:`os.PathLike` |
| 221 | is accepted:: |
| 222 | |
| 223 | >>> import os |
| 224 | >>> p = PurePath('/etc') |
| 225 | >>> os.fspath(p) |
| 226 | '/etc' |
| 227 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 228 | The string representation of a path is the raw filesystem path itself |
| 229 | (in native form, e.g. with backslashes under Windows), which you can |
| 230 | pass 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 | |
| 239 | Similarly, calling :class:`bytes` on a path gives the raw filesystem path as a |
| 240 | bytes 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 | |
| 250 | Accessing individual parts |
| 251 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 252 | |
| 253 | To access the individual "parts" (components) of a path, use the following |
| 254 | property: |
| 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 | |
| 271 | Methods and properties |
| 272 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 273 | |
Andrew Kuchling | 7a4e2d1 | 2013-11-22 15:45:02 -0500 | [diff] [blame] | 274 | Pure paths provide the following methods and properties: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 275 | |
| 276 | .. data:: PurePath.drive |
| 277 | |
| 278 | A string representing the drive letter or name, if any:: |
| 279 | |
| 280 | >>> PureWindowsPath('c:/Program Files/').drive |
| 281 | 'c:' |
| 282 | >>> PureWindowsPath('/Program Files/').drive |
| 283 | '' |
| 284 | >>> PurePosixPath('/etc').drive |
| 285 | '' |
| 286 | |
| 287 | UNC shares are also considered drives:: |
| 288 | |
| 289 | >>> PureWindowsPath('//host/share/foo.txt').drive |
| 290 | '\\\\host\\share' |
| 291 | |
| 292 | .. data:: PurePath.root |
| 293 | |
| 294 | A string representing the (local or global) root, if any:: |
| 295 | |
| 296 | >>> PureWindowsPath('c:/Program Files/').root |
| 297 | '\\' |
| 298 | >>> PureWindowsPath('c:Program Files/').root |
| 299 | '' |
| 300 | >>> PurePosixPath('/etc').root |
| 301 | '/' |
| 302 | |
| 303 | UNC shares always have a root:: |
| 304 | |
| 305 | >>> PureWindowsPath('//host/share').root |
| 306 | '\\' |
| 307 | |
| 308 | .. data:: PurePath.anchor |
| 309 | |
| 310 | The concatenation of the drive and root:: |
| 311 | |
| 312 | >>> PureWindowsPath('c:/Program Files/').anchor |
| 313 | 'c:\\' |
| 314 | >>> PureWindowsPath('c:Program Files/').anchor |
| 315 | 'c:' |
| 316 | >>> PurePosixPath('/etc').anchor |
| 317 | '/' |
| 318 | >>> PureWindowsPath('//host/share').anchor |
| 319 | '\\\\host\\share\\' |
| 320 | |
| 321 | |
| 322 | .. data:: PurePath.parents |
| 323 | |
| 324 | An immutable sequence providing access to the logical ancestors of |
| 325 | the path:: |
| 326 | |
| 327 | >>> p = PureWindowsPath('c:/foo/bar/setup.py') |
| 328 | >>> p.parents[0] |
| 329 | PureWindowsPath('c:/foo/bar') |
| 330 | >>> p.parents[1] |
| 331 | PureWindowsPath('c:/foo') |
| 332 | >>> p.parents[2] |
| 333 | PureWindowsPath('c:/') |
| 334 | |
| 335 | |
| 336 | .. data:: PurePath.parent |
| 337 | |
| 338 | The logical parent of the path:: |
| 339 | |
| 340 | >>> p = PurePosixPath('/a/b/c/d') |
| 341 | >>> p.parent |
| 342 | PurePosixPath('/a/b/c') |
| 343 | |
| 344 | You cannot go past an anchor, or empty path:: |
| 345 | |
| 346 | >>> p = PurePosixPath('/') |
| 347 | >>> p.parent |
| 348 | PurePosixPath('/') |
| 349 | >>> p = PurePosixPath('.') |
| 350 | >>> p.parent |
| 351 | PurePosixPath('.') |
| 352 | |
| 353 | .. note:: |
| 354 | This is a purely lexical operation, hence the following behaviour:: |
| 355 | |
| 356 | >>> p = PurePosixPath('foo/..') |
| 357 | >>> p.parent |
| 358 | PurePosixPath('foo') |
| 359 | |
| 360 | If you want to walk an arbitrary filesystem path upwards, it is |
| 361 | recommended to first call :meth:`Path.resolve` so as to resolve |
| 362 | symlinks and eliminate `".."` components. |
| 363 | |
| 364 | |
| 365 | .. data:: PurePath.name |
| 366 | |
| 367 | A string representing the final path component, excluding the drive and |
| 368 | root, if any:: |
| 369 | |
| 370 | >>> PurePosixPath('my/library/setup.py').name |
| 371 | 'setup.py' |
| 372 | |
| 373 | UNC drive names are not considered:: |
| 374 | |
| 375 | >>> PureWindowsPath('//some/share/setup.py').name |
| 376 | 'setup.py' |
| 377 | >>> PureWindowsPath('//some/share').name |
| 378 | '' |
| 379 | |
| 380 | |
| 381 | .. data:: PurePath.suffix |
| 382 | |
| 383 | The file extension of the final component, if any:: |
| 384 | |
| 385 | >>> PurePosixPath('my/library/setup.py').suffix |
| 386 | '.py' |
| 387 | >>> PurePosixPath('my/library.tar.gz').suffix |
| 388 | '.gz' |
| 389 | >>> PurePosixPath('my/library').suffix |
| 390 | '' |
| 391 | |
| 392 | |
| 393 | .. data:: PurePath.suffixes |
| 394 | |
| 395 | A list of the path's file extensions:: |
| 396 | |
| 397 | >>> PurePosixPath('my/library.tar.gar').suffixes |
| 398 | ['.tar', '.gar'] |
| 399 | >>> PurePosixPath('my/library.tar.gz').suffixes |
| 400 | ['.tar', '.gz'] |
| 401 | >>> PurePosixPath('my/library').suffixes |
| 402 | [] |
| 403 | |
| 404 | |
| 405 | .. data:: PurePath.stem |
| 406 | |
| 407 | The final path component, without its suffix:: |
| 408 | |
| 409 | >>> PurePosixPath('my/library.tar.gz').stem |
| 410 | 'library.tar' |
| 411 | >>> PurePosixPath('my/library.tar').stem |
| 412 | 'library' |
| 413 | >>> PurePosixPath('my/library').stem |
| 414 | 'library' |
| 415 | |
| 416 | |
| 417 | .. method:: PurePath.as_posix() |
| 418 | |
| 419 | Return a string representation of the path with forward slashes (``/``):: |
| 420 | |
| 421 | >>> p = PureWindowsPath('c:\\windows') |
| 422 | >>> str(p) |
| 423 | 'c:\\windows' |
| 424 | >>> p.as_posix() |
| 425 | 'c:/windows' |
| 426 | |
| 427 | |
| 428 | .. method:: PurePath.as_uri() |
| 429 | |
| 430 | Represent the path as a ``file`` URI. :exc:`ValueError` is raised if |
| 431 | the path isn't absolute. |
| 432 | |
| 433 | >>> p = PurePosixPath('/etc/passwd') |
| 434 | >>> p.as_uri() |
| 435 | 'file:///etc/passwd' |
| 436 | >>> p = PureWindowsPath('c:/Windows') |
| 437 | >>> p.as_uri() |
| 438 | 'file:///c:/Windows' |
| 439 | |
| 440 | |
| 441 | .. method:: PurePath.is_absolute() |
| 442 | |
| 443 | Return whether the path is absolute or not. A path is considered absolute |
| 444 | if it has both a root and (if the flavour allows) a drive:: |
| 445 | |
| 446 | >>> PurePosixPath('/a/b').is_absolute() |
| 447 | True |
| 448 | >>> PurePosixPath('a/b').is_absolute() |
| 449 | False |
| 450 | |
| 451 | >>> PureWindowsPath('c:/a/b').is_absolute() |
| 452 | True |
| 453 | >>> PureWindowsPath('/a/b').is_absolute() |
| 454 | False |
| 455 | >>> PureWindowsPath('c:').is_absolute() |
| 456 | False |
| 457 | >>> PureWindowsPath('//some/share').is_absolute() |
| 458 | True |
| 459 | |
| 460 | |
| 461 | .. method:: PurePath.is_reserved() |
| 462 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 463 | With :class:`PureWindowsPath`, return ``True`` if the path is considered |
| 464 | reserved under Windows, ``False`` otherwise. With :class:`PurePosixPath`, |
| 465 | ``False`` is always returned. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 466 | |
| 467 | >>> PureWindowsPath('nul').is_reserved() |
| 468 | True |
| 469 | >>> PurePosixPath('nul').is_reserved() |
| 470 | False |
| 471 | |
| 472 | File system calls on reserved paths can fail mysteriously or have |
| 473 | unintended effects. |
| 474 | |
| 475 | |
| 476 | .. method:: PurePath.joinpath(*other) |
| 477 | |
Andrew Kuchling | 7a4e2d1 | 2013-11-22 15:45:02 -0500 | [diff] [blame] | 478 | Calling this method is equivalent to combining the path with each of |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 479 | the *other* arguments in turn:: |
| 480 | |
| 481 | >>> PurePosixPath('/etc').joinpath('passwd') |
| 482 | PurePosixPath('/etc/passwd') |
| 483 | >>> PurePosixPath('/etc').joinpath(PurePosixPath('passwd')) |
| 484 | PurePosixPath('/etc/passwd') |
| 485 | >>> PurePosixPath('/etc').joinpath('init.d', 'apache2') |
| 486 | PurePosixPath('/etc/init.d/apache2') |
| 487 | >>> PureWindowsPath('c:').joinpath('/Program Files') |
| 488 | PureWindowsPath('c:/Program Files') |
| 489 | |
| 490 | |
| 491 | .. method:: PurePath.match(pattern) |
| 492 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 493 | Match this path against the provided glob-style pattern. Return ``True`` |
| 494 | if matching is successful, ``False`` otherwise. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 495 | |
| 496 | If *pattern* is relative, the path can be either relative or absolute, |
| 497 | and matching is done from the right:: |
| 498 | |
| 499 | >>> PurePath('a/b.py').match('*.py') |
| 500 | True |
| 501 | >>> PurePath('/a/b/c.py').match('b/*.py') |
| 502 | True |
| 503 | >>> PurePath('/a/b/c.py').match('a/*.py') |
| 504 | False |
| 505 | |
| 506 | If *pattern* is absolute, the path must be absolute, and the whole path |
| 507 | must match:: |
| 508 | |
| 509 | >>> PurePath('/a.py').match('/*.py') |
| 510 | True |
| 511 | >>> PurePath('a/b.py').match('/*.py') |
| 512 | False |
| 513 | |
| 514 | As with other methods, case-sensitivity is observed:: |
| 515 | |
| 516 | >>> PureWindowsPath('b.py').match('*.PY') |
| 517 | True |
| 518 | |
| 519 | |
| 520 | .. method:: PurePath.relative_to(*other) |
| 521 | |
| 522 | Compute a version of this path relative to the path represented by |
| 523 | *other*. If it's impossible, ValueError is raised:: |
| 524 | |
| 525 | >>> p = PurePosixPath('/etc/passwd') |
| 526 | >>> p.relative_to('/') |
| 527 | PurePosixPath('etc/passwd') |
| 528 | >>> p.relative_to('/etc') |
| 529 | PurePosixPath('passwd') |
| 530 | >>> p.relative_to('/usr') |
| 531 | Traceback (most recent call last): |
| 532 | File "<stdin>", line 1, in <module> |
| 533 | File "pathlib.py", line 694, in relative_to |
| 534 | .format(str(self), str(formatted))) |
| 535 | ValueError: '/etc/passwd' does not start with '/usr' |
| 536 | |
| 537 | |
Antoine Pitrou | ef85119 | 2014-02-25 20:33:02 +0100 | [diff] [blame] | 538 | .. method:: PurePath.with_name(name) |
| 539 | |
| 540 | Return a new path with the :attr:`name` changed. If the original path |
| 541 | doesn't have a name, ValueError is raised:: |
| 542 | |
| 543 | >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz') |
| 544 | >>> p.with_name('setup.py') |
| 545 | PureWindowsPath('c:/Downloads/setup.py') |
| 546 | >>> p = PureWindowsPath('c:/') |
| 547 | >>> p.with_name('setup.py') |
| 548 | Traceback (most recent call last): |
| 549 | File "<stdin>", line 1, in <module> |
| 550 | File "/home/antoine/cpython/default/Lib/pathlib.py", line 751, in with_name |
| 551 | raise ValueError("%r has an empty name" % (self,)) |
| 552 | ValueError: PureWindowsPath('c:/') has an empty name |
| 553 | |
| 554 | |
| 555 | .. method:: PurePath.with_suffix(suffix) |
| 556 | |
| 557 | Return a new path with the :attr:`suffix` changed. If the original path |
| 558 | doesn't have a suffix, the new *suffix* is appended instead:: |
| 559 | |
| 560 | >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz') |
| 561 | >>> p.with_suffix('.bz2') |
| 562 | PureWindowsPath('c:/Downloads/pathlib.tar.bz2') |
| 563 | >>> p = PureWindowsPath('README') |
| 564 | >>> p.with_suffix('.txt') |
| 565 | PureWindowsPath('README.txt') |
| 566 | |
| 567 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 568 | .. _concrete-paths: |
| 569 | |
| 570 | |
| 571 | Concrete paths |
| 572 | -------------- |
| 573 | |
| 574 | Concrete paths are subclasses of the pure path classes. In addition to |
| 575 | operations provided by the latter, they also provide methods to do system |
| 576 | calls on path objects. There are three ways to instantiate concrete paths: |
| 577 | |
Eli Bendersky | b6e66eb | 2013-11-28 06:53:05 -0800 | [diff] [blame] | 578 | .. class:: Path(*pathsegments) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 579 | |
| 580 | A subclass of :class:`PurePath`, this class represents concrete paths of |
| 581 | the system's path flavour (instantiating it creates either a |
| 582 | :class:`PosixPath` or a :class:`WindowsPath`):: |
| 583 | |
| 584 | >>> Path('setup.py') |
| 585 | PosixPath('setup.py') |
| 586 | |
Eli Bendersky | b6e66eb | 2013-11-28 06:53:05 -0800 | [diff] [blame] | 587 | *pathsegments* is specified similarly to :class:`PurePath`. |
| 588 | |
| 589 | .. class:: PosixPath(*pathsegments) |
| 590 | |
| 591 | A subclass of :class:`Path` and :class:`PurePosixPath`, this class |
| 592 | represents concrete non-Windows filesystem paths:: |
| 593 | |
| 594 | >>> PosixPath('/etc') |
| 595 | PosixPath('/etc') |
| 596 | |
| 597 | *pathsegments* is specified similarly to :class:`PurePath`. |
| 598 | |
| 599 | .. class:: WindowsPath(*pathsegments) |
| 600 | |
| 601 | A subclass of :class:`Path` and :class:`PureWindowsPath`, this class |
| 602 | represents concrete Windows filesystem paths:: |
| 603 | |
| 604 | >>> WindowsPath('c:/Program Files/') |
| 605 | WindowsPath('c:/Program Files') |
| 606 | |
| 607 | *pathsegments* is specified similarly to :class:`PurePath`. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 608 | |
| 609 | You can only instantiate the class flavour that corresponds to your system |
| 610 | (allowing system calls on non-compatible path flavours could lead to |
| 611 | bugs or failures in your application):: |
| 612 | |
| 613 | >>> import os |
| 614 | >>> os.name |
| 615 | 'posix' |
| 616 | >>> Path('setup.py') |
| 617 | PosixPath('setup.py') |
| 618 | >>> PosixPath('setup.py') |
| 619 | PosixPath('setup.py') |
| 620 | >>> WindowsPath('setup.py') |
| 621 | Traceback (most recent call last): |
| 622 | File "<stdin>", line 1, in <module> |
| 623 | File "pathlib.py", line 798, in __new__ |
| 624 | % (cls.__name__,)) |
| 625 | NotImplementedError: cannot instantiate 'WindowsPath' on your system |
| 626 | |
| 627 | |
| 628 | Methods |
| 629 | ^^^^^^^ |
| 630 | |
| 631 | Concrete paths provide the following methods in addition to pure paths |
| 632 | methods. Many of these methods can raise an :exc:`OSError` if a system |
| 633 | call fails (for example because the path doesn't exist): |
| 634 | |
| 635 | .. classmethod:: Path.cwd() |
| 636 | |
| 637 | Return a new path object representing the current directory (as returned |
| 638 | by :func:`os.getcwd`):: |
| 639 | |
| 640 | >>> Path.cwd() |
| 641 | PosixPath('/home/antoine/pathlib') |
| 642 | |
| 643 | |
Antoine Pitrou | 17cba7d | 2015-01-12 21:03:41 +0100 | [diff] [blame] | 644 | .. classmethod:: Path.home() |
| 645 | |
| 646 | Return a new path object representing the user's home directory (as |
| 647 | returned by :func:`os.path.expanduser` with ``~`` construct):: |
| 648 | |
| 649 | >>> Path.home() |
| 650 | PosixPath('/home/antoine') |
| 651 | |
| 652 | .. versionadded:: 3.5 |
| 653 | |
| 654 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 655 | .. method:: Path.stat() |
| 656 | |
| 657 | Return information about this path (similarly to :func:`os.stat`). |
| 658 | The result is looked up at each call to this method. |
| 659 | |
| 660 | >>> p = Path('setup.py') |
| 661 | >>> p.stat().st_size |
| 662 | 956 |
| 663 | >>> p.stat().st_mtime |
| 664 | 1327883547.852554 |
| 665 | |
| 666 | |
| 667 | .. method:: Path.chmod(mode) |
| 668 | |
| 669 | Change the file mode and permissions, like :func:`os.chmod`:: |
| 670 | |
| 671 | >>> p = Path('setup.py') |
| 672 | >>> p.stat().st_mode |
| 673 | 33277 |
| 674 | >>> p.chmod(0o444) |
| 675 | >>> p.stat().st_mode |
| 676 | 33060 |
| 677 | |
| 678 | |
| 679 | .. method:: Path.exists() |
| 680 | |
| 681 | Whether the path points to an existing file or directory:: |
| 682 | |
| 683 | >>> Path('.').exists() |
| 684 | True |
| 685 | >>> Path('setup.py').exists() |
| 686 | True |
| 687 | >>> Path('/etc').exists() |
| 688 | True |
| 689 | >>> Path('nonexistentfile').exists() |
| 690 | False |
| 691 | |
| 692 | .. note:: |
| 693 | If the path points to a symlink, :meth:`exists` returns whether the |
| 694 | symlink *points to* an existing file or directory. |
| 695 | |
| 696 | |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 697 | .. method:: Path.expanduser() |
| 698 | |
| 699 | Return a new path with expanded ``~`` and ``~user`` constructs, |
| 700 | as returned by :meth:`os.path.expanduser`:: |
| 701 | |
| 702 | >>> p = PosixPath('~/films/Monty Python') |
| 703 | >>> p.expanduser() |
| 704 | PosixPath('/home/eric/films/Monty Python') |
| 705 | |
| 706 | .. versionadded:: 3.5 |
| 707 | |
| 708 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 709 | .. method:: Path.glob(pattern) |
| 710 | |
| 711 | Glob the given *pattern* in the directory represented by this path, |
| 712 | yielding all matching files (of any kind):: |
| 713 | |
| 714 | >>> sorted(Path('.').glob('*.py')) |
| 715 | [PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')] |
| 716 | >>> sorted(Path('.').glob('*/*.py')) |
| 717 | [PosixPath('docs/conf.py')] |
| 718 | |
| 719 | The "``**``" pattern means "this directory and all subdirectories, |
| 720 | recursively". In other words, it enables recursive globbing:: |
| 721 | |
| 722 | >>> sorted(Path('.').glob('**/*.py')) |
| 723 | [PosixPath('build/lib/pathlib.py'), |
| 724 | PosixPath('docs/conf.py'), |
| 725 | PosixPath('pathlib.py'), |
| 726 | PosixPath('setup.py'), |
| 727 | PosixPath('test_pathlib.py')] |
| 728 | |
| 729 | .. note:: |
| 730 | Using the "``**``" pattern in large directory trees may consume |
| 731 | an inordinate amount of time. |
| 732 | |
| 733 | |
| 734 | .. method:: Path.group() |
| 735 | |
Ned Deily | c034156 | 2013-11-27 14:42:55 -0800 | [diff] [blame] | 736 | Return the name of the group owning the file. :exc:`KeyError` is raised |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 737 | if the file's gid isn't found in the system database. |
| 738 | |
| 739 | |
| 740 | .. method:: Path.is_dir() |
| 741 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 742 | Return ``True`` if the path points to a directory (or a symbolic link |
| 743 | pointing to a directory), ``False`` if it points to another kind of file. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 744 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 745 | ``False`` is also returned if the path doesn't exist or is a broken symlink; |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 746 | other errors (such as permission errors) are propagated. |
| 747 | |
| 748 | |
| 749 | .. method:: Path.is_file() |
| 750 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 751 | Return ``True`` if the path points to a regular file (or a symbolic link |
| 752 | pointing to a regular file), ``False`` if it points to another kind of file. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 753 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 754 | ``False`` is also returned if the path doesn't exist or is a broken symlink; |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 755 | other errors (such as permission errors) are propagated. |
| 756 | |
| 757 | |
| 758 | .. method:: Path.is_symlink() |
| 759 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 760 | Return ``True`` if the path points to a symbolic link, ``False`` otherwise. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 761 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 762 | ``False`` is also returned if the path doesn't exist; other errors (such |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 763 | as permission errors) are propagated. |
| 764 | |
| 765 | |
| 766 | .. method:: Path.is_socket() |
| 767 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 768 | Return ``True`` if the path points to a Unix socket (or a symbolic link |
| 769 | pointing to a Unix socket), ``False`` if it points to another kind of file. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 770 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 771 | ``False`` is also returned if the path doesn't exist or is a broken symlink; |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 772 | other errors (such as permission errors) are propagated. |
| 773 | |
| 774 | |
| 775 | .. method:: Path.is_fifo() |
| 776 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 777 | Return ``True`` if the path points to a FIFO (or a symbolic link |
| 778 | pointing to a FIFO), ``False`` if it points to another kind of file. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 779 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 780 | ``False`` is also returned if the path doesn't exist or is a broken symlink; |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 781 | other errors (such as permission errors) are propagated. |
| 782 | |
| 783 | |
| 784 | .. method:: Path.is_block_device() |
| 785 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 786 | Return ``True`` if the path points to a block device (or a symbolic link |
| 787 | pointing to a block device), ``False`` if it points to another kind of file. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 788 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 789 | ``False`` is also returned if the path doesn't exist or is a broken symlink; |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 790 | other errors (such as permission errors) are propagated. |
| 791 | |
| 792 | |
| 793 | .. method:: Path.is_char_device() |
| 794 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 795 | Return ``True`` if the path points to a character device (or a symbolic link |
| 796 | pointing to a character device), ``False`` if it points to another kind of file. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 797 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 798 | ``False`` is also returned if the path doesn't exist or is a broken symlink; |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 799 | other errors (such as permission errors) are propagated. |
| 800 | |
| 801 | |
| 802 | .. method:: Path.iterdir() |
| 803 | |
| 804 | When the path points to a directory, yield path objects of the directory |
| 805 | contents:: |
| 806 | |
| 807 | >>> p = Path('docs') |
| 808 | >>> for child in p.iterdir(): child |
| 809 | ... |
| 810 | PosixPath('docs/conf.py') |
| 811 | PosixPath('docs/_templates') |
| 812 | PosixPath('docs/make.bat') |
| 813 | PosixPath('docs/index.rst') |
| 814 | PosixPath('docs/_build') |
| 815 | PosixPath('docs/_static') |
| 816 | PosixPath('docs/Makefile') |
| 817 | |
| 818 | .. method:: Path.lchmod(mode) |
| 819 | |
| 820 | Like :meth:`Path.chmod` but, if the path points to a symbolic link, the |
| 821 | symbolic link's mode is changed rather than its target's. |
| 822 | |
| 823 | |
| 824 | .. method:: Path.lstat() |
| 825 | |
| 826 | Like :meth:`Path.stat` but, if the path points to a symbolic link, return |
| 827 | the symbolic link's information rather than its target's. |
| 828 | |
| 829 | |
Barry Warsaw | 7c549c4 | 2014-08-05 11:28:12 -0400 | [diff] [blame] | 830 | .. method:: Path.mkdir(mode=0o777, parents=False, exist_ok=False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 831 | |
| 832 | Create a new directory at this given path. If *mode* is given, it is |
| 833 | combined with the process' ``umask`` value to determine the file mode |
Antoine Pitrou | f6abb70 | 2013-12-16 21:00:53 +0100 | [diff] [blame] | 834 | and access flags. If the path already exists, :exc:`FileExistsError` |
| 835 | is raised. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 836 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 837 | If *parents* is true, any missing parents of this path are created |
Antoine Pitrou | 0048c98 | 2013-12-16 20:22:37 +0100 | [diff] [blame] | 838 | as needed; they are created with the default permissions without taking |
| 839 | *mode* into account (mimicking the POSIX ``mkdir -p`` command). |
| 840 | |
| 841 | If *parents* is false (the default), a missing parent raises |
Antoine Pitrou | f6abb70 | 2013-12-16 21:00:53 +0100 | [diff] [blame] | 842 | :exc:`FileNotFoundError`. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 843 | |
Ned Deily | 11194f7 | 2016-10-15 15:12:03 -0400 | [diff] [blame] | 844 | If *exist_ok* is false (the default), :exc:`FileExistsError` is |
Barry Warsaw | 7c549c4 | 2014-08-05 11:28:12 -0400 | [diff] [blame] | 845 | raised if the target directory already exists. |
| 846 | |
| 847 | If *exist_ok* is true, :exc:`FileExistsError` exceptions will be |
| 848 | ignored (same behavior as the POSIX ``mkdir -p`` command), but only if the |
| 849 | last path component is not an existing non-directory file. |
| 850 | |
| 851 | .. versionchanged:: 3.5 |
| 852 | The *exist_ok* parameter was added. |
| 853 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 854 | |
| 855 | .. method:: Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None) |
| 856 | |
| 857 | Open the file pointed to by the path, like the built-in :func:`open` |
| 858 | function does:: |
| 859 | |
| 860 | >>> p = Path('setup.py') |
| 861 | >>> with p.open() as f: |
| 862 | ... f.readline() |
| 863 | ... |
| 864 | '#!/usr/bin/env python3\n' |
| 865 | |
| 866 | |
| 867 | .. method:: Path.owner() |
| 868 | |
Ned Deily | c034156 | 2013-11-27 14:42:55 -0800 | [diff] [blame] | 869 | Return the name of the user owning the file. :exc:`KeyError` is raised |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 870 | if the file's uid isn't found in the system database. |
| 871 | |
| 872 | |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 873 | .. method:: Path.read_bytes() |
| 874 | |
| 875 | Return the binary contents of the pointed-to file as a bytes object:: |
| 876 | |
| 877 | >>> p = Path('my_binary_file') |
| 878 | >>> p.write_bytes(b'Binary file contents') |
| 879 | 20 |
| 880 | >>> p.read_bytes() |
| 881 | b'Binary file contents' |
| 882 | |
| 883 | .. versionadded:: 3.5 |
| 884 | |
| 885 | |
| 886 | .. method:: Path.read_text(encoding=None, errors=None) |
| 887 | |
| 888 | Return the decoded contents of the pointed-to file as a string:: |
| 889 | |
| 890 | >>> p = Path('my_text_file') |
| 891 | >>> p.write_text('Text file contents') |
| 892 | 18 |
| 893 | >>> p.read_text() |
| 894 | 'Text file contents' |
| 895 | |
| 896 | The optional parameters have the same meaning as in :func:`open`. |
| 897 | |
| 898 | .. versionadded:: 3.5 |
| 899 | |
| 900 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 901 | .. method:: Path.rename(target) |
| 902 | |
Berker Peksag | 2b87921 | 2016-07-14 07:44:59 +0300 | [diff] [blame] | 903 | Rename this file or directory to the given *target*. On Unix, if |
| 904 | *target* exists and is a file, it will be replaced silently if the user |
| 905 | has permission. *target* can be either a string or another path object:: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 906 | |
| 907 | >>> p = Path('foo') |
| 908 | >>> p.open('w').write('some text') |
| 909 | 9 |
| 910 | >>> target = Path('bar') |
| 911 | >>> p.rename(target) |
| 912 | >>> target.open().read() |
| 913 | 'some text' |
| 914 | |
| 915 | |
| 916 | .. method:: Path.replace(target) |
| 917 | |
| 918 | Rename this file or directory to the given *target*. If *target* points |
| 919 | to an existing file or directory, it will be unconditionally replaced. |
| 920 | |
| 921 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 922 | .. method:: Path.resolve(strict=False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 923 | |
| 924 | Make the path absolute, resolving any symlinks. A new path object is |
| 925 | returned:: |
| 926 | |
| 927 | >>> p = Path() |
| 928 | >>> p |
| 929 | PosixPath('.') |
| 930 | >>> p.resolve() |
| 931 | PosixPath('/home/antoine/pathlib') |
| 932 | |
Berker Peksag | 5e3677d | 2016-10-01 01:06:52 +0300 | [diff] [blame] | 933 | "``..``" components are also eliminated (this is the only method to do so):: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 934 | |
| 935 | >>> p = Path('docs/../setup.py') |
| 936 | >>> p.resolve() |
| 937 | PosixPath('/home/antoine/pathlib/setup.py') |
| 938 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 939 | If the path doesn't exist and *strict* is ``True``, :exc:`FileNotFoundError` |
| 940 | is raised. If *strict* is ``False``, the path is resolved as far as possible |
| 941 | and any remainder is appended without checking whether it exists. If an |
| 942 | infinite loop is encountered along the resolution path, :exc:`RuntimeError` |
| 943 | is raised. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 944 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 945 | .. versionadded:: 3.6 |
| 946 | The *strict* argument. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 947 | |
| 948 | .. method:: Path.rglob(pattern) |
| 949 | |
Berker Peksag | 06a8ac0 | 2016-10-01 01:02:39 +0300 | [diff] [blame] | 950 | This is like calling :meth:`Path.glob` with "``**``" added in front of the |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 951 | given *pattern*: |
| 952 | |
| 953 | >>> sorted(Path().rglob("*.py")) |
| 954 | [PosixPath('build/lib/pathlib.py'), |
| 955 | PosixPath('docs/conf.py'), |
| 956 | PosixPath('pathlib.py'), |
| 957 | PosixPath('setup.py'), |
| 958 | PosixPath('test_pathlib.py')] |
| 959 | |
| 960 | |
| 961 | .. method:: Path.rmdir() |
| 962 | |
| 963 | Remove this directory. The directory must be empty. |
| 964 | |
| 965 | |
Antoine Pitrou | 43e3d94 | 2014-05-13 10:50:15 +0200 | [diff] [blame] | 966 | .. method:: Path.samefile(other_path) |
| 967 | |
| 968 | Return whether this path points to the same file as *other_path*, which |
| 969 | can be either a Path object, or a string. The semantics are similar |
| 970 | to :func:`os.path.samefile` and :func:`os.path.samestat`. |
| 971 | |
| 972 | An :exc:`OSError` can be raised if either file cannot be accessed for some |
| 973 | reason. |
| 974 | |
| 975 | >>> p = Path('spam') |
| 976 | >>> q = Path('eggs') |
| 977 | >>> p.samefile(q) |
| 978 | False |
| 979 | >>> p.samefile('spam') |
| 980 | True |
| 981 | |
| 982 | .. versionadded:: 3.5 |
| 983 | |
| 984 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 985 | .. method:: Path.symlink_to(target, target_is_directory=False) |
| 986 | |
| 987 | Make this path a symbolic link to *target*. Under Windows, |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 988 | *target_is_directory* must be true (default ``False``) if the link's target |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 989 | is a directory. Under POSIX, *target_is_directory*'s value is ignored. |
| 990 | |
| 991 | >>> p = Path('mylink') |
| 992 | >>> p.symlink_to('setup.py') |
| 993 | >>> p.resolve() |
| 994 | PosixPath('/home/antoine/pathlib/setup.py') |
| 995 | >>> p.stat().st_size |
| 996 | 956 |
| 997 | >>> p.lstat().st_size |
| 998 | 8 |
| 999 | |
| 1000 | .. note:: |
| 1001 | The order of arguments (link, target) is the reverse |
| 1002 | of :func:`os.symlink`'s. |
| 1003 | |
| 1004 | |
Zachary Ware | 7a26da5 | 2016-08-09 17:10:39 -0500 | [diff] [blame] | 1005 | .. method:: Path.touch(mode=0o666, exist_ok=True) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1006 | |
| 1007 | Create a file at this given path. If *mode* is given, it is combined |
| 1008 | with the process' ``umask`` value to determine the file mode and access |
| 1009 | flags. If the file already exists, the function succeeds if *exist_ok* |
| 1010 | is true (and its modification time is updated to the current time), |
Antoine Pitrou | f6abb70 | 2013-12-16 21:00:53 +0100 | [diff] [blame] | 1011 | otherwise :exc:`FileExistsError` is raised. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1012 | |
| 1013 | |
| 1014 | .. method:: Path.unlink() |
| 1015 | |
| 1016 | Remove this file or symbolic link. If the path points to a directory, |
| 1017 | use :func:`Path.rmdir` instead. |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 1018 | |
| 1019 | |
| 1020 | .. method:: Path.write_bytes(data) |
| 1021 | |
| 1022 | Open the file pointed to in bytes mode, write *data* to it, and close the |
| 1023 | file:: |
| 1024 | |
| 1025 | >>> p = Path('my_binary_file') |
| 1026 | >>> p.write_bytes(b'Binary file contents') |
| 1027 | 20 |
| 1028 | >>> p.read_bytes() |
| 1029 | b'Binary file contents' |
| 1030 | |
| 1031 | An existing file of the same name is overwritten. |
| 1032 | |
| 1033 | .. versionadded:: 3.5 |
| 1034 | |
| 1035 | |
| 1036 | .. method:: Path.write_text(data, encoding=None, errors=None) |
| 1037 | |
| 1038 | Open the file pointed to in text mode, write *data* to it, and close the |
| 1039 | file:: |
| 1040 | |
| 1041 | >>> p = Path('my_text_file') |
| 1042 | >>> p.write_text('Text file contents') |
| 1043 | 18 |
| 1044 | >>> p.read_text() |
| 1045 | 'Text file contents' |
| 1046 | |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 1047 | .. versionadded:: 3.5 |