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
| 817 | |
| 818 | |
| 819 | class Path(PurePath): |
| 820 | """PurePath subclass that can make system calls. |
| 821 | |
| 822 | Path represents a filesystem path but unlike PurePath, also offers |
| 823 | methods to do system calls on path objects. Depending on your system, |
| 824 | instantiating a Path will return either a PosixPath or a WindowsPath |
| 825 | object. You can also instantiate a PosixPath or WindowsPath directly, |
| 826 | but cannot instantiate a WindowsPath on a POSIX system or vice versa. |
| 827 | """ |
| 828 | __slots__ = ('_info',) |
| 829 | |
| 830 | def __new__(cls, *args, **kwargs): |
| 831 | if cls is Path: |
| 832 | cls = WindowsPath if os.name == 'nt' else PosixPath |
| 833 | return object.__new__(cls) |
| 834 | |
| 835 | @property |
| 836 | def info(self): |
| 837 | """ |
| 838 | A PathInfo object that exposes the file type and other file attributes |
| 839 | of this path. |
| 840 | """ |
| 841 | try: |
| 842 | return self._info |
| 843 | except AttributeError: |
| 844 | self._info = _Info(str(self)) |
| 845 | return self._info |
| 846 | |
| 847 | def stat(self, *, follow_symlinks=True): |
| 848 | """ |
| 849 | Return the result of the stat() system call on this path, like |
| 850 | os.stat() does. |
| 851 | """ |
| 852 | return os.stat(self, follow_symlinks=follow_symlinks) |
| 853 | |
| 854 | def lstat(self): |
| 855 | """ |
| 856 | Like stat(), except if the path points to a symlink, the symlink's |
| 857 | status information is returned, rather than its target's. |
| 858 | """ |
| 859 | return os.lstat(self) |
| 860 | |
| 861 | def exists(self, *, follow_symlinks=True): |
| 862 | """ |
| 863 | Whether this path exists. |
| 864 | |
| 865 | This method normally follows symlinks; to check whether a symlink exists, |
| 866 | add the argument follow_symlinks=False. |
| 867 | """ |
| 868 | if follow_symlinks: |
| 869 | return os.path.exists(self) |
| 870 | return os.path.lexists(self) |
| 871 | |
| 872 | def is_dir(self, *, follow_symlinks=True): |
| 873 | """ |
| 874 | Whether this path is a directory. |
| 875 | """ |
| 876 | if follow_symlinks: |
searching dependent graphs…