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 insta
| 855 | |
| 856 | |
| 857 | class Path(PurePath): |
| 858 | """PurePath subclass that can make system calls. |
| 859 | |
| 860 | Path represents a filesystem path but unlike PurePath, also offers |
| 861 | methods to do system calls on path objects. Depending on your system, |
| 862 | instantiating a Path will return either a PosixPath or a WindowsPath |
| 863 | object. You can also instantiate a PosixPath or WindowsPath directly, |
| 864 | but cannot instantiate a WindowsPath on a POSIX system or vice versa. |
| 865 | """ |
| 866 | __slots__ = () |
| 867 | |
| 868 | def __new__(cls, *args, **kwargs): |
| 869 | if cls is Path: |
| 870 | cls = WindowsPath if os.name == 'nt' else PosixPath |
| 871 | self = cls._from_parts(args) |
| 872 | if not self._flavour.is_supported: |
| 873 | raise NotImplementedError("cannot instantiate %r on your system" |
| 874 | % (cls.__name__,)) |
| 875 | return self |
| 876 | |
| 877 | def _make_child_relpath(self, part): |
| 878 | # This is an optimization used for dir walking. `part` must be |
| 879 | # a single part relative to this path. |
| 880 | parts = self._parts + [part] |
| 881 | return self._from_parsed_parts(self._drv, self._root, parts) |
| 882 | |
| 883 | def __enter__(self): |
| 884 | # In previous versions of pathlib, __exit__() marked this path as |
| 885 | # closed; subsequent attempts to perform I/O would raise an IOError. |
| 886 | # This functionality was never documented, and had the effect of |
| 887 | # making Path objects mutable, contrary to PEP 428. |
| 888 | # In Python 3.9 __exit__() was made a no-op. |
| 889 | # In Python 3.11 __enter__() began emitting DeprecationWarning. |
| 890 | # In Python 3.13 __enter__() and __exit__() should be removed. |
| 891 | warnings.warn("pathlib.Path.__enter__() is deprecated and scheduled " |
| 892 | "for removal in Python 3.13; Path objects as a context " |
| 893 | "manager is a no-op", |
| 894 | DeprecationWarning, stacklevel=2) |
| 895 | return self |
| 896 | |
| 897 | def __exit__(self, t, v, tb): |
| 898 | pass |
| 899 | |
| 900 | # Public API |
| 901 | |
| 902 | @classmethod |
| 903 | def cwd(cls): |
| 904 | """Return a new path pointing to the current working directory |
| 905 | (as returned by os.getcwd()). |
| 906 | """ |
| 907 | return cls(os.getcwd()) |
| 908 | |
| 909 | @classmethod |
| 910 | def home(cls): |
| 911 | """Return a new path pointing to the user's home directory (as |
| 912 | returned by os.path.expanduser('~')). |
| 913 | """ |
| 914 | return cls("~").expanduser() |
no outgoing calls
no test coverage detected