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