classification
Title: pathlib.Path: glob and rglob should accept PathLike patterns
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ciupicri, nanjekyejoannah, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-12-10 10:54 by ciupicri, last changed 2019-05-29 00:16 by nanjekyejoannah.

Messages (5)
msg331491 - (view) Author: Cristian Ciupitu (ciupicri) * Date: 2018-12-10 10:54
pathlib.Path.glob and pathlib.Path.rglob don't work with os.PathLike patterns.

Short example:

from pathlib import Path, PurePath
# fails
tuple(Path('/etc').glob(PurePath('passwd')))    # TypeError
tuple(Path('/etc').rglob(PurePath('passwd')))   # TypeError
tuple(Path('C:\\').glob(PurePath('Windows')))   # AttributeError
tuple(Path('C:\\').rglob(PurePath('Windows')))  # AttributeError
# works
from os import fspath
tuple(Path('/etc').glob(fspath(PurePath('passwd'))))
tuple(Path('/etc').rglob(fspath(PurePath('passwd'))))
tuple(Path('C:\\').glob(fspath(PurePath('Windows'))))
tuple(Path('C:\\').rglob(fspath(PurePath('Windows'))))
msg331503 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-12-10 12:24
This is for purpose. Path is not a glob pattern, and glob pattern is not a path. '*.txt' is aт ordinary file name on Linux and is invalid file name on Windows. If we wanted to accept Path in any place that accepts a string, we would make Path a subclass of str.
msg331507 - (view) Author: Cristian Ciupitu (ciupicri) * Date: 2018-12-10 13:07
What if the pattern has some directories in it, e.g. "SourceArt/**/*.png", how do you compose it? The traditional way is to either hardcode the separator (e.g. / or \) or use os.path.combine. I don't see why PurePath can't be used for this, e.g. PurePath('SourceArt')/"**'/'*.png'.
msg331567 - (view) Author: Cristian Ciupitu (ciupicri) * Date: 2018-12-11 04:06
Err, I meant os.path.join instead of os.path.combine.
msg343840 - (view) Author: Joannah Nanjekye (nanjekyejoannah) * (Python triager) Date: 2019-05-29 00:16
Since this is not considered an issue, I will just add tests to confirm this behavior and close this.
History
Date User Action Args
2019-05-29 00:16:03nanjekyejoannahsetnosy: + nanjekyejoannah
messages: + msg343840
2018-12-14 20:45:38terry.reedysetstage: test needed
type: enhancement
versions: + Python 3.8, - Python 3.7
2018-12-11 04:06:34ciupicrisetmessages: + msg331567
2018-12-10 13:07:21ciupicrisetmessages: + msg331507
2018-12-10 12:24:55serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg331503
2018-12-10 10:54:42ciupicricreate