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 | |
Marco Buttu | 7b2491a | 2017-04-13 16:17:59 +0200 | [diff] [blame] | 274 | .. testsetup:: |
| 275 | |
| 276 | from pathlib import PurePosixPath, PureWindowsPath |
| 277 | |
Andrew Kuchling | 7a4e2d1 | 2013-11-22 15:45:02 -0500 | [diff] [blame] | 278 | Pure paths provide the following methods and properties: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 279 | |
| 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 | |
| 465 | .. method:: PurePath.is_reserved() |
| 466 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 467 | With :class:`PureWindowsPath`, return ``True`` if the path is considered |
| 468 | reserved under Windows, ``False`` otherwise. With :class:`PurePosixPath`, |
| 469 | ``False`` is always returned. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 470 | |
| 471 | >>> PureWindowsPath('nul').is_reserved() |
| 472 | True |
| 473 | >>> PurePosixPath('nul').is_reserved() |
| 474 | False |
| 475 | |
| 476 | File system calls on reserved paths can fail mysteriously or have |
| 477 | unintended effects. |
| 478 | |
| 479 | |
| 480 | .. method:: PurePath.joinpath(*other) |
| 481 | |
Andrew Kuchling | 7a4e2d1 | 2013-11-22 15:45:02 -0500 | [diff] [blame] | 482 | Calling this method is equivalent to combining the path with each of |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 483 | the *other* arguments in turn:: |
| 484 | |
| 485 | >>> PurePosixPath('/etc').joinpath('passwd') |
| 486 | PurePosixPath('/etc/passwd') |
| 487 | >>> PurePosixPath('/etc').joinpath(PurePosixPath('passwd')) |
| 488 | PurePosixPath('/etc/passwd') |
| 489 | >>> PurePosixPath('/etc').joinpath('init.d', 'apache2') |
| 490 | PurePosixPath('/etc/init.d/apache2') |
| 491 | >>> PureWindowsPath('c:').joinpath('/Program Files') |
| 492 | PureWindowsPath('c:/Program Files') |
| 493 | |
| 494 | |
| 495 | .. method:: PurePath.match(pattern) |
| 496 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 497 | Match this path against the provided glob-style pattern. Return ``True`` |
| 498 | if matching is successful, ``False`` otherwise. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 499 | |
| 500 | If *pattern* is relative, the path can be either relative or absolute, |
| 501 | and matching is done from the right:: |
| 502 | |
| 503 | >>> PurePath('a/b.py').match('*.py') |
| 504 | True |
| 505 | >>> PurePath('/a/b/c.py').match('b/*.py') |
| 506 | True |
| 507 | >>> PurePath('/a/b/c.py').match('a/*.py') |
| 508 | False |
| 509 | |
| 510 | If *pattern* is absolute, the path must be absolute, and the whole path |
| 511 | must match:: |
| 512 | |
| 513 | >>> PurePath('/a.py').match('/*.py') |
| 514 | True |
| 515 | >>> PurePath('a/b.py').match('/*.py') |
| 516 | False |
| 517 | |
| 518 | As with other methods, case-sensitivity is observed:: |
| 519 | |
| 520 | >>> PureWindowsPath('b.py').match('*.PY') |
| 521 | True |
| 522 | |
| 523 | |
| 524 | .. method:: PurePath.relative_to(*other) |
| 525 | |
| 526 | Compute a version of this path relative to the path represented by |
| 527 | *other*. If it's impossible, ValueError is raised:: |
| 528 | |
| 529 | >>> p = PurePosixPath('/etc/passwd') |
| 530 | >>> p.relative_to('/') |
| 531 | PurePosixPath('etc/passwd') |
| 532 | >>> p.relative_to('/etc') |
| 533 | PurePosixPath('passwd') |
| 534 | >>> p.relative_to('/usr') |
| 535 | Traceback (most recent call last): |
| 536 | File "<stdin>", line 1, in <module> |
| 537 | File "pathlib.py", line 694, in relative_to |
| 538 | .format(str(self), str(formatted))) |
| 539 | ValueError: '/etc/passwd' does not start with '/usr' |
| 540 | |
| 541 | |
Antoine Pitrou | ef85119 | 2014-02-25 20:33:02 +0100 | [diff] [blame] | 542 | .. method:: PurePath.with_name(name) |
| 543 | |
| 544 | Return a new path with the :attr:`name` changed. If the original path |
| 545 | doesn't have a name, ValueError is raised:: |
| 546 | |
| 547 | >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz') |
| 548 | >>> p.with_name('setup.py') |
| 549 | PureWindowsPath('c:/Downloads/setup.py') |
| 550 | >>> p = PureWindowsPath('c:/') |
| 551 | >>> p.with_name('setup.py') |
| 552 | Traceback (most recent call last): |
| 553 | File "<stdin>", line 1, in <module> |
| 554 | File "/home/antoine/cpython/default/Lib/pathlib.py", line 751, in with_name |
| 555 | raise ValueError("%r has an empty name" % (self,)) |
| 556 | ValueError: PureWindowsPath('c:/') has an empty name |
| 557 | |
| 558 | |
| 559 | .. method:: PurePath.with_suffix(suffix) |
| 560 | |
| 561 | Return a new path with the :attr:`suffix` changed. If the original path |
Stefan Otte | 46dc4e3 | 2018-08-03 22:49:42 +0200 | [diff] [blame] | 562 | doesn't have a suffix, the new *suffix* is appended instead. If the |
| 563 | *suffix* is an empty string, the original suffix is removed:: |
Antoine Pitrou | ef85119 | 2014-02-25 20:33:02 +0100 | [diff] [blame] | 564 | |
| 565 | >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz') |
| 566 | >>> p.with_suffix('.bz2') |
| 567 | PureWindowsPath('c:/Downloads/pathlib.tar.bz2') |
| 568 | >>> p = PureWindowsPath('README') |
| 569 | >>> p.with_suffix('.txt') |
| 570 | PureWindowsPath('README.txt') |
Stefan Otte | 46dc4e3 | 2018-08-03 22:49:42 +0200 | [diff] [blame] | 571 | >>> p = PureWindowsPath('README.txt') |
| 572 | >>> p.with_suffix('') |
| 573 | PureWindowsPath('README') |
Antoine Pitrou | ef85119 | 2014-02-25 20:33:02 +0100 | [diff] [blame] | 574 | |
| 575 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 576 | .. _concrete-paths: |
| 577 | |
| 578 | |
| 579 | Concrete paths |
| 580 | -------------- |
| 581 | |
| 582 | Concrete paths are subclasses of the pure path classes. In addition to |
| 583 | operations provided by the latter, they also provide methods to do system |
| 584 | calls on path objects. There are three ways to instantiate concrete paths: |
| 585 | |
Eli Bendersky | b6e66eb | 2013-11-28 06:53:05 -0800 | [diff] [blame] | 586 | .. class:: Path(*pathsegments) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 587 | |
| 588 | A subclass of :class:`PurePath`, this class represents concrete paths of |
| 589 | the system's path flavour (instantiating it creates either a |
| 590 | :class:`PosixPath` or a :class:`WindowsPath`):: |
| 591 | |
| 592 | >>> Path('setup.py') |
| 593 | PosixPath('setup.py') |
| 594 | |
Eli Bendersky | b6e66eb | 2013-11-28 06:53:05 -0800 | [diff] [blame] | 595 | *pathsegments* is specified similarly to :class:`PurePath`. |
| 596 | |
| 597 | .. class:: PosixPath(*pathsegments) |
| 598 | |
| 599 | A subclass of :class:`Path` and :class:`PurePosixPath`, this class |
| 600 | represents concrete non-Windows filesystem paths:: |
| 601 | |
| 602 | >>> PosixPath('/etc') |
| 603 | PosixPath('/etc') |
| 604 | |
| 605 | *pathsegments* is specified similarly to :class:`PurePath`. |
| 606 | |
| 607 | .. class:: WindowsPath(*pathsegments) |
| 608 | |
| 609 | A subclass of :class:`Path` and :class:`PureWindowsPath`, this class |
| 610 | represents concrete Windows filesystem paths:: |
| 611 | |
| 612 | >>> WindowsPath('c:/Program Files/') |
| 613 | WindowsPath('c:/Program Files') |
| 614 | |
| 615 | *pathsegments* is specified similarly to :class:`PurePath`. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 616 | |
| 617 | You can only instantiate the class flavour that corresponds to your system |
| 618 | (allowing system calls on non-compatible path flavours could lead to |
| 619 | bugs or failures in your application):: |
| 620 | |
| 621 | >>> import os |
| 622 | >>> os.name |
| 623 | 'posix' |
| 624 | >>> Path('setup.py') |
| 625 | PosixPath('setup.py') |
| 626 | >>> PosixPath('setup.py') |
| 627 | PosixPath('setup.py') |
| 628 | >>> WindowsPath('setup.py') |
| 629 | Traceback (most recent call last): |
| 630 | File "<stdin>", line 1, in <module> |
| 631 | File "pathlib.py", line 798, in __new__ |
| 632 | % (cls.__name__,)) |
| 633 | NotImplementedError: cannot instantiate 'WindowsPath' on your system |
| 634 | |
| 635 | |
| 636 | Methods |
| 637 | ^^^^^^^ |
| 638 | |
| 639 | Concrete paths provide the following methods in addition to pure paths |
| 640 | methods. Many of these methods can raise an :exc:`OSError` if a system |
| 641 | call fails (for example because the path doesn't exist): |
| 642 | |
| 643 | .. classmethod:: Path.cwd() |
| 644 | |
| 645 | Return a new path object representing the current directory (as returned |
| 646 | by :func:`os.getcwd`):: |
| 647 | |
| 648 | >>> Path.cwd() |
| 649 | PosixPath('/home/antoine/pathlib') |
| 650 | |
| 651 | |
Antoine Pitrou | 17cba7d | 2015-01-12 21:03:41 +0100 | [diff] [blame] | 652 | .. classmethod:: Path.home() |
| 653 | |
| 654 | Return a new path object representing the user's home directory (as |
| 655 | returned by :func:`os.path.expanduser` with ``~`` construct):: |
| 656 | |
| 657 | >>> Path.home() |
| 658 | PosixPath('/home/antoine') |
| 659 | |
| 660 | .. versionadded:: 3.5 |
| 661 | |
| 662 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 663 | .. method:: Path.stat() |
| 664 | |
| 665 | Return information about this path (similarly to :func:`os.stat`). |
| 666 | The result is looked up at each call to this method. |
| 667 | |
Marco Buttu | 7b2491a | 2017-04-13 16:17:59 +0200 | [diff] [blame] | 668 | :: |
| 669 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 670 | >>> p = Path('setup.py') |
| 671 | >>> p.stat().st_size |
| 672 | 956 |
| 673 | >>> p.stat().st_mtime |
| 674 | 1327883547.852554 |
| 675 | |
| 676 | |
| 677 | .. method:: Path.chmod(mode) |
| 678 | |
| 679 | Change the file mode and permissions, like :func:`os.chmod`:: |
| 680 | |
| 681 | >>> p = Path('setup.py') |
| 682 | >>> p.stat().st_mode |
| 683 | 33277 |
| 684 | >>> p.chmod(0o444) |
| 685 | >>> p.stat().st_mode |
| 686 | 33060 |
| 687 | |
| 688 | |
| 689 | .. method:: Path.exists() |
| 690 | |
| 691 | Whether the path points to an existing file or directory:: |
| 692 | |
| 693 | >>> Path('.').exists() |
| 694 | True |
| 695 | >>> Path('setup.py').exists() |
| 696 | True |
| 697 | >>> Path('/etc').exists() |
| 698 | True |
| 699 | >>> Path('nonexistentfile').exists() |
| 700 | False |
| 701 | |
| 702 | .. note:: |
| 703 | If the path points to a symlink, :meth:`exists` returns whether the |
| 704 | symlink *points to* an existing file or directory. |
| 705 | |
| 706 | |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 707 | .. method:: Path.expanduser() |
| 708 | |
| 709 | Return a new path with expanded ``~`` and ``~user`` constructs, |
| 710 | as returned by :meth:`os.path.expanduser`:: |
| 711 | |
| 712 | >>> p = PosixPath('~/films/Monty Python') |
| 713 | >>> p.expanduser() |
| 714 | PosixPath('/home/eric/films/Monty Python') |
| 715 | |
| 716 | .. versionadded:: 3.5 |
| 717 | |
| 718 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 719 | .. method:: Path.glob(pattern) |
| 720 | |
| 721 | Glob the given *pattern* in the directory represented by this path, |
| 722 | yielding all matching files (of any kind):: |
| 723 | |
| 724 | >>> sorted(Path('.').glob('*.py')) |
| 725 | [PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')] |
| 726 | >>> sorted(Path('.').glob('*/*.py')) |
| 727 | [PosixPath('docs/conf.py')] |
| 728 | |
| 729 | The "``**``" pattern means "this directory and all subdirectories, |
| 730 | recursively". In other words, it enables recursive globbing:: |
| 731 | |
| 732 | >>> sorted(Path('.').glob('**/*.py')) |
| 733 | [PosixPath('build/lib/pathlib.py'), |
| 734 | PosixPath('docs/conf.py'), |
| 735 | PosixPath('pathlib.py'), |
| 736 | PosixPath('setup.py'), |
| 737 | PosixPath('test_pathlib.py')] |
| 738 | |
| 739 | .. note:: |
| 740 | Using the "``**``" pattern in large directory trees may consume |
| 741 | an inordinate amount of time. |
| 742 | |
| 743 | |
| 744 | .. method:: Path.group() |
| 745 | |
Ned Deily | c034156 | 2013-11-27 14:42:55 -0800 | [diff] [blame] | 746 | 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] | 747 | if the file's gid isn't found in the system database. |
| 748 | |
| 749 | |
| 750 | .. method:: Path.is_dir() |
| 751 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 752 | Return ``True`` if the path points to a directory (or a symbolic link |
| 753 | 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] | 754 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 755 | ``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] | 756 | other errors (such as permission errors) are propagated. |
| 757 | |
| 758 | |
| 759 | .. method:: Path.is_file() |
| 760 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 761 | Return ``True`` if the path points to a regular file (or a symbolic link |
| 762 | 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] | 763 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 764 | ``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] | 765 | other errors (such as permission errors) are propagated. |
| 766 | |
| 767 | |
Łukasz Langa | 47320a6 | 2017-08-01 16:47:50 -0700 | [diff] [blame] | 768 | .. method:: Path.is_mount() |
| 769 | |
| 770 | Return ``True`` if the path is a :dfn:`mount point`: a point in a |
| 771 | file system where a different file system has been mounted. On POSIX, the |
| 772 | function checks whether *path*'s parent, :file:`path/..`, is on a different |
| 773 | device than *path*, or whether :file:`path/..` and *path* point to the same |
| 774 | i-node on the same device --- this should detect mount points for all Unix |
| 775 | and POSIX variants. Not implemented on Windows. |
| 776 | |
| 777 | .. versionadded:: 3.7 |
| 778 | |
| 779 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 780 | .. method:: Path.is_symlink() |
| 781 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 782 | Return ``True`` if the path points to a symbolic link, ``False`` otherwise. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 783 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 784 | ``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] | 785 | as permission errors) are propagated. |
| 786 | |
| 787 | |
| 788 | .. method:: Path.is_socket() |
| 789 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 790 | Return ``True`` if the path points to a Unix socket (or a symbolic link |
| 791 | 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] | 792 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 793 | ``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] | 794 | other errors (such as permission errors) are propagated. |
| 795 | |
| 796 | |
| 797 | .. method:: Path.is_fifo() |
| 798 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 799 | Return ``True`` if the path points to a FIFO (or a symbolic link |
| 800 | 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] | 801 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 802 | ``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] | 803 | other errors (such as permission errors) are propagated. |
| 804 | |
| 805 | |
| 806 | .. method:: Path.is_block_device() |
| 807 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 808 | Return ``True`` if the path points to a block device (or a symbolic link |
| 809 | 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] | 810 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 811 | ``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] | 812 | other errors (such as permission errors) are propagated. |
| 813 | |
| 814 | |
| 815 | .. method:: Path.is_char_device() |
| 816 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 817 | Return ``True`` if the path points to a character device (or a symbolic link |
| 818 | 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] | 819 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 820 | ``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] | 821 | other errors (such as permission errors) are propagated. |
| 822 | |
| 823 | |
| 824 | .. method:: Path.iterdir() |
| 825 | |
| 826 | When the path points to a directory, yield path objects of the directory |
| 827 | contents:: |
| 828 | |
| 829 | >>> p = Path('docs') |
| 830 | >>> for child in p.iterdir(): child |
| 831 | ... |
| 832 | PosixPath('docs/conf.py') |
| 833 | PosixPath('docs/_templates') |
| 834 | PosixPath('docs/make.bat') |
| 835 | PosixPath('docs/index.rst') |
| 836 | PosixPath('docs/_build') |
| 837 | PosixPath('docs/_static') |
| 838 | PosixPath('docs/Makefile') |
| 839 | |
| 840 | .. method:: Path.lchmod(mode) |
| 841 | |
| 842 | Like :meth:`Path.chmod` but, if the path points to a symbolic link, the |
| 843 | symbolic link's mode is changed rather than its target's. |
| 844 | |
| 845 | |
| 846 | .. method:: Path.lstat() |
| 847 | |
| 848 | Like :meth:`Path.stat` but, if the path points to a symbolic link, return |
| 849 | the symbolic link's information rather than its target's. |
| 850 | |
| 851 | |
Barry Warsaw | 7c549c4 | 2014-08-05 11:28:12 -0400 | [diff] [blame] | 852 | .. method:: Path.mkdir(mode=0o777, parents=False, exist_ok=False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 853 | |
| 854 | Create a new directory at this given path. If *mode* is given, it is |
| 855 | combined with the process' ``umask`` value to determine the file mode |
Antoine Pitrou | f6abb70 | 2013-12-16 21:00:53 +0100 | [diff] [blame] | 856 | and access flags. If the path already exists, :exc:`FileExistsError` |
| 857 | is raised. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 858 | |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 859 | If *parents* is true, any missing parents of this path are created |
Antoine Pitrou | 0048c98 | 2013-12-16 20:22:37 +0100 | [diff] [blame] | 860 | as needed; they are created with the default permissions without taking |
| 861 | *mode* into account (mimicking the POSIX ``mkdir -p`` command). |
| 862 | |
| 863 | If *parents* is false (the default), a missing parent raises |
Antoine Pitrou | f6abb70 | 2013-12-16 21:00:53 +0100 | [diff] [blame] | 864 | :exc:`FileNotFoundError`. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 865 | |
Ned Deily | 11194f7 | 2016-10-15 15:12:03 -0400 | [diff] [blame] | 866 | If *exist_ok* is false (the default), :exc:`FileExistsError` is |
Barry Warsaw | 7c549c4 | 2014-08-05 11:28:12 -0400 | [diff] [blame] | 867 | raised if the target directory already exists. |
| 868 | |
| 869 | If *exist_ok* is true, :exc:`FileExistsError` exceptions will be |
| 870 | ignored (same behavior as the POSIX ``mkdir -p`` command), but only if the |
| 871 | last path component is not an existing non-directory file. |
| 872 | |
| 873 | .. versionchanged:: 3.5 |
| 874 | The *exist_ok* parameter was added. |
| 875 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 876 | |
| 877 | .. method:: Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None) |
| 878 | |
| 879 | Open the file pointed to by the path, like the built-in :func:`open` |
| 880 | function does:: |
| 881 | |
| 882 | >>> p = Path('setup.py') |
| 883 | >>> with p.open() as f: |
| 884 | ... f.readline() |
| 885 | ... |
| 886 | '#!/usr/bin/env python3\n' |
| 887 | |
| 888 | |
| 889 | .. method:: Path.owner() |
| 890 | |
Ned Deily | c034156 | 2013-11-27 14:42:55 -0800 | [diff] [blame] | 891 | 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] | 892 | if the file's uid isn't found in the system database. |
| 893 | |
| 894 | |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 895 | .. method:: Path.read_bytes() |
| 896 | |
| 897 | Return the binary contents of the pointed-to file as a bytes object:: |
| 898 | |
| 899 | >>> p = Path('my_binary_file') |
| 900 | >>> p.write_bytes(b'Binary file contents') |
| 901 | 20 |
| 902 | >>> p.read_bytes() |
| 903 | b'Binary file contents' |
| 904 | |
| 905 | .. versionadded:: 3.5 |
| 906 | |
| 907 | |
| 908 | .. method:: Path.read_text(encoding=None, errors=None) |
| 909 | |
| 910 | Return the decoded contents of the pointed-to file as a string:: |
| 911 | |
| 912 | >>> p = Path('my_text_file') |
| 913 | >>> p.write_text('Text file contents') |
| 914 | 18 |
| 915 | >>> p.read_text() |
| 916 | 'Text file contents' |
| 917 | |
Xtreak | 5b2657f | 2018-08-07 01:25:03 +0530 | [diff] [blame] | 918 | The file is opened and then closed. The optional parameters have the same |
| 919 | meaning as in :func:`open`. |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 920 | |
| 921 | .. versionadded:: 3.5 |
| 922 | |
| 923 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 924 | .. method:: Path.rename(target) |
| 925 | |
Berker Peksag | 2b87921 | 2016-07-14 07:44:59 +0300 | [diff] [blame] | 926 | Rename this file or directory to the given *target*. On Unix, if |
| 927 | *target* exists and is a file, it will be replaced silently if the user |
| 928 | has permission. *target* can be either a string or another path object:: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 929 | |
| 930 | >>> p = Path('foo') |
| 931 | >>> p.open('w').write('some text') |
| 932 | 9 |
| 933 | >>> target = Path('bar') |
| 934 | >>> p.rename(target) |
| 935 | >>> target.open().read() |
| 936 | 'some text' |
| 937 | |
| 938 | |
| 939 | .. method:: Path.replace(target) |
| 940 | |
| 941 | Rename this file or directory to the given *target*. If *target* points |
| 942 | to an existing file or directory, it will be unconditionally replaced. |
| 943 | |
| 944 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 945 | .. method:: Path.resolve(strict=False) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 946 | |
| 947 | Make the path absolute, resolving any symlinks. A new path object is |
| 948 | returned:: |
| 949 | |
| 950 | >>> p = Path() |
| 951 | >>> p |
| 952 | PosixPath('.') |
| 953 | >>> p.resolve() |
| 954 | PosixPath('/home/antoine/pathlib') |
| 955 | |
Berker Peksag | 5e3677d | 2016-10-01 01:06:52 +0300 | [diff] [blame] | 956 | "``..``" components are also eliminated (this is the only method to do so):: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 957 | |
| 958 | >>> p = Path('docs/../setup.py') |
| 959 | >>> p.resolve() |
| 960 | PosixPath('/home/antoine/pathlib/setup.py') |
| 961 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 962 | If the path doesn't exist and *strict* is ``True``, :exc:`FileNotFoundError` |
| 963 | is raised. If *strict* is ``False``, the path is resolved as far as possible |
| 964 | and any remainder is appended without checking whether it exists. If an |
| 965 | infinite loop is encountered along the resolution path, :exc:`RuntimeError` |
| 966 | is raised. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 967 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 968 | .. versionadded:: 3.6 |
| 969 | The *strict* argument. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 970 | |
| 971 | .. method:: Path.rglob(pattern) |
| 972 | |
Berker Peksag | 06a8ac0 | 2016-10-01 01:02:39 +0300 | [diff] [blame] | 973 | This is like calling :meth:`Path.glob` with "``**``" added in front of the |
Marco Buttu | 7b2491a | 2017-04-13 16:17:59 +0200 | [diff] [blame] | 974 | given *pattern*:: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 975 | |
| 976 | >>> sorted(Path().rglob("*.py")) |
| 977 | [PosixPath('build/lib/pathlib.py'), |
| 978 | PosixPath('docs/conf.py'), |
| 979 | PosixPath('pathlib.py'), |
| 980 | PosixPath('setup.py'), |
| 981 | PosixPath('test_pathlib.py')] |
| 982 | |
| 983 | |
| 984 | .. method:: Path.rmdir() |
| 985 | |
| 986 | Remove this directory. The directory must be empty. |
| 987 | |
| 988 | |
Antoine Pitrou | 43e3d94 | 2014-05-13 10:50:15 +0200 | [diff] [blame] | 989 | .. method:: Path.samefile(other_path) |
| 990 | |
| 991 | Return whether this path points to the same file as *other_path*, which |
| 992 | can be either a Path object, or a string. The semantics are similar |
| 993 | to :func:`os.path.samefile` and :func:`os.path.samestat`. |
| 994 | |
| 995 | An :exc:`OSError` can be raised if either file cannot be accessed for some |
| 996 | reason. |
| 997 | |
Marco Buttu | 7b2491a | 2017-04-13 16:17:59 +0200 | [diff] [blame] | 998 | :: |
| 999 | |
Antoine Pitrou | 43e3d94 | 2014-05-13 10:50:15 +0200 | [diff] [blame] | 1000 | >>> p = Path('spam') |
| 1001 | >>> q = Path('eggs') |
| 1002 | >>> p.samefile(q) |
| 1003 | False |
| 1004 | >>> p.samefile('spam') |
| 1005 | True |
| 1006 | |
| 1007 | .. versionadded:: 3.5 |
| 1008 | |
| 1009 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1010 | .. method:: Path.symlink_to(target, target_is_directory=False) |
| 1011 | |
| 1012 | Make this path a symbolic link to *target*. Under Windows, |
Serhiy Storchaka | 03cc565 | 2013-11-26 21:37:12 +0200 | [diff] [blame] | 1013 | *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] | 1014 | is a directory. Under POSIX, *target_is_directory*'s value is ignored. |
| 1015 | |
Marco Buttu | 7b2491a | 2017-04-13 16:17:59 +0200 | [diff] [blame] | 1016 | :: |
| 1017 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1018 | >>> p = Path('mylink') |
| 1019 | >>> p.symlink_to('setup.py') |
| 1020 | >>> p.resolve() |
| 1021 | PosixPath('/home/antoine/pathlib/setup.py') |
| 1022 | >>> p.stat().st_size |
| 1023 | 956 |
| 1024 | >>> p.lstat().st_size |
| 1025 | 8 |
| 1026 | |
| 1027 | .. note:: |
| 1028 | The order of arguments (link, target) is the reverse |
| 1029 | of :func:`os.symlink`'s. |
| 1030 | |
| 1031 | |
Zachary Ware | 7a26da5 | 2016-08-09 17:10:39 -0500 | [diff] [blame] | 1032 | .. method:: Path.touch(mode=0o666, exist_ok=True) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1033 | |
| 1034 | Create a file at this given path. If *mode* is given, it is combined |
| 1035 | with the process' ``umask`` value to determine the file mode and access |
| 1036 | flags. If the file already exists, the function succeeds if *exist_ok* |
| 1037 | is true (and its modification time is updated to the current time), |
Antoine Pitrou | f6abb70 | 2013-12-16 21:00:53 +0100 | [diff] [blame] | 1038 | otherwise :exc:`FileExistsError` is raised. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1039 | |
| 1040 | |
| 1041 | .. method:: Path.unlink() |
| 1042 | |
| 1043 | Remove this file or symbolic link. If the path points to a directory, |
| 1044 | use :func:`Path.rmdir` instead. |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 1045 | |
| 1046 | |
| 1047 | .. method:: Path.write_bytes(data) |
| 1048 | |
| 1049 | Open the file pointed to in bytes mode, write *data* to it, and close the |
| 1050 | file:: |
| 1051 | |
| 1052 | >>> p = Path('my_binary_file') |
| 1053 | >>> p.write_bytes(b'Binary file contents') |
| 1054 | 20 |
| 1055 | >>> p.read_bytes() |
| 1056 | b'Binary file contents' |
| 1057 | |
| 1058 | An existing file of the same name is overwritten. |
| 1059 | |
| 1060 | .. versionadded:: 3.5 |
| 1061 | |
| 1062 | |
| 1063 | .. method:: Path.write_text(data, encoding=None, errors=None) |
| 1064 | |
| 1065 | Open the file pointed to in text mode, write *data* to it, and close the |
| 1066 | file:: |
| 1067 | |
| 1068 | >>> p = Path('my_text_file') |
| 1069 | >>> p.write_text('Text file contents') |
| 1070 | 18 |
| 1071 | >>> p.read_text() |
| 1072 | 'Text file contents' |
| 1073 | |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 1074 | .. versionadded:: 3.5 |
Jamiel Almeida | ae8750b | 2017-06-02 11:36:02 -0700 | [diff] [blame] | 1075 | |
| 1076 | Correspondence to tools in the :mod:`os` module |
| 1077 | ----------------------------------------------- |
| 1078 | |
| 1079 | Below is a table mapping various :mod:`os` functions to their corresponding |
| 1080 | :class:`PurePath`/:class:`Path` equivalent. |
| 1081 | |
| 1082 | .. note:: |
| 1083 | |
| 1084 | Although :func:`os.path.relpath` and :meth:`PurePath.relative_to` have some |
| 1085 | overlapping use-cases, their semantics differ enough to warrant not |
| 1086 | considering them equivalent. |
| 1087 | |
| 1088 | ============================ ============================== |
| 1089 | os and os.path pathlib |
| 1090 | ============================ ============================== |
| 1091 | :func:`os.path.abspath` :meth:`Path.resolve` |
| 1092 | :func:`os.getcwd` :func:`Path.cwd` |
| 1093 | :func:`os.path.exists` :meth:`Path.exists` |
| 1094 | :func:`os.path.expanduser` :meth:`Path.expanduser` and |
| 1095 | :meth:`Path.home` |
| 1096 | :func:`os.path.isdir` :meth:`Path.is_dir` |
| 1097 | :func:`os.path.isfile` :meth:`Path.is_file` |
| 1098 | :func:`os.path.islink` :meth:`Path.is_symlink` |
| 1099 | :func:`os.stat` :meth:`Path.stat`, |
| 1100 | :meth:`Path.owner`, |
| 1101 | :meth:`Path.group` |
| 1102 | :func:`os.path.isabs` :meth:`PurePath.is_absolute` |
| 1103 | :func:`os.path.join` :func:`PurePath.joinpath` |
| 1104 | :func:`os.path.basename` :data:`PurePath.name` |
| 1105 | :func:`os.path.dirname` :data:`PurePath.parent` |
| 1106 | :func:`os.path.splitext` :data:`PurePath.suffix` |
| 1107 | ============================ ============================== |