[New-bugs-announce] [issue44979] pathlib: support relative path construction

Kirill Pinchuk report at bugs.python.org
Sun Aug 22 11:07:54 EDT 2021


New submission from Kirill Pinchuk <cybergrind at gmail.com>:

Hi. I've been using this snippet for years and believe that it would be a nice addition to pathlib's functionality.

Basically, it allows constructing path relative to the current file (instead of cwd). Comes quite handy when you're working with deeply nested resources like file fixtures in tests and many other cases.

```
    @classmethod
    def relative(cls, path, depth=1):
        """
        Return path that is constructed relatively to caller file.
        """
        base = Path(sys._getframe(depth).f_code.co_filename).parent
        return (base / path).resolve()
```

----------
components: Library (Lib)
messages: 400075
nosy: cybergrind
priority: normal
severity: normal
status: open
title: pathlib: support relative path construction
type: enhancement
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44979>
_______________________________________


More information about the New-bugs-announce mailing list