PurePath subclass that can make system calls. Path represents a filesystem path but unlike PurePath, also offers methods to do system calls on path objects. Depending on your system, instantiating a Path will return either a PosixPath or a WindowsPath object. You can also instantiat
| 619 | |
| 620 | |
| 621 | class Path(PurePath): |
| 622 | """PurePath subclass that can make system calls. |
| 623 | |
| 624 | Path represents a filesystem path but unlike PurePath, also offers |
| 625 | methods to do system calls on path objects. Depending on your system, |
| 626 | instantiating a Path will return either a PosixPath or a WindowsPath |
| 627 | object. You can also instantiate a PosixPath or WindowsPath directly, |
| 628 | but cannot instantiate a WindowsPath on a POSIX system or vice versa. |
| 629 | """ |
| 630 | __slots__ = ('_info',) |
| 631 | |
| 632 | def __new__(cls, *args, **kwargs): |
| 633 | if cls is Path: |
| 634 | cls = WindowsPath if os.name == 'nt' else PosixPath |
| 635 | return object.__new__(cls) |
| 636 | |
| 637 | @property |
| 638 | def info(self): |
| 639 | """ |
| 640 | A PathInfo object that exposes the file type and other file attributes |
| 641 | of this path. |
| 642 | """ |
| 643 | try: |
| 644 | return self._info |
| 645 | except AttributeError: |
| 646 | self._info = PathInfo(self) |
| 647 | return self._info |
| 648 | |
| 649 | def stat(self, *, follow_symlinks=True): |
| 650 | """ |
| 651 | Return the result of the stat() system call on this path, like |
| 652 | os.stat() does. |
| 653 | """ |
| 654 | return os.stat(self, follow_symlinks=follow_symlinks) |
| 655 | |
| 656 | def lstat(self): |
| 657 | """ |
| 658 | Like stat(), except if the path points to a symlink, the symlink's |
| 659 | status information is returned, rather than its target's. |
| 660 | """ |
| 661 | return os.lstat(self) |
| 662 | |
| 663 | def exists(self, *, follow_symlinks=True): |
| 664 | """ |
| 665 | Whether this path exists. |
| 666 | |
| 667 | This method normally follows symlinks; to check whether a symlink exists, |
| 668 | add the argument follow_symlinks=False. |
| 669 | """ |
| 670 | if follow_symlinks: |
| 671 | return os.path.exists(self) |
| 672 | return os.path.lexists(self) |
| 673 | |
| 674 | def is_dir(self, *, follow_symlinks=True): |
| 675 | """ |
| 676 | Whether this path is a directory. |
| 677 | """ |
| 678 | if follow_symlinks: |