[New-bugs-announce] [issue42758] pathlib.Path to support the "in" operator (x in y)

Anton Hvornum report at bugs.python.org
Sun Dec 27 13:54:36 EST 2020


New submission from Anton Hvornum <anton.feeds+github at gmail.com>:

I would like to propose that the `pathlib.Path()` gets a `in` operator, much like that ipaddress has IP in Subnet, it would be nice if we could be able to do:

```
import pathlib
pathlib.Path('/home/Torxed/machine.qcow2')
pathlib.Path('/home/Torxed/machine.qcow2') in pathlib.Path('/home/Torxed')
```

Currently that would generate:
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'PosixPath' is not iterable
```

This would avoid "complicated" implementations such as:
 * https://stackoverflow.com/questions/21411904/python-how-to-check-if-path-is-a-subpath
 * https://stackoverflow.com/questions/3812849/how-to-check-whether-a-directory-is-a-sub-directory-of-another-directory

Which tend to be half-complete truths and would result in potential security issues. pathlib.Path() could help prevent some of those.

----------
components: Library (Lib)
messages: 383857
nosy: Torxed
priority: normal
severity: normal
status: open
title: pathlib.Path to support the "in" operator (x in y)
type: enhancement
versions: Python 3.10, Python 3.9

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


More information about the New-bugs-announce mailing list