Python glob but against a list of strings rather than the filesystem

后端 未结 9 1765
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 21:24

I want to be able to match a pattern in glob format to a list of strings, rather than to actual files in the filesystem. Is there any way to do this, or convert a glob

9条回答
  •  情书的邮戳
    2021-02-06 21:45

    On Python 3.4+ you can just use PurePath.match.

    pathlib.PurePath(path_string).match(pattern)
    

    On Python 3.3 or earlier (including 2.x), get pathlib from PyPI.

    Note that to get platform-independent results (which will depend on why you're running this) you'd want to explicitly state PurePosixPath or PureWindowsPath.

提交回复
热议问题