当 glob 以斜杠结尾时,如何防止 pathlib 的 Path.glob 返回文件?
How to prevent pathlib’s Path.glob from returning files when the glob ends in a slash?
当 glob 模式以斜线结尾时,pathlib
中的新 Path.glob
似乎与旧 glob.glob
的行为不同。
In [1]: from pathlib import Path
In [2]: from glob import glob
In [3]: glob('webroot/*/')
Out[3]: ['webroot/2017-06-07/']
In [4]: list(Path().glob('webroot/*/'))
Out[4]:
[PosixPath('webroot/.keep'),
PosixPath('webroot/2017-06-07'),
PosixPath('webroot/matches.2017-06-07.json')]
这是设计使然,我没有遇到过一些兼容性问题吗?有没有办法阻止它这样做?
现在我将解决这个问题:
[path for path in Path().glob('webroot/*/') if path.is_dir()]
有一个关于此的未解决的错误:
尚未解决。
您的解决方法看起来不错,但如果您不介意还包括 'webroot' 目录本身,您可能更喜欢使用 **
glob:
>>> list(Path('webroot').glob('**'))
[PosixPath('webroot'), PosixPath('webroot/2017-06-07')]
当 glob 模式以斜线结尾时,pathlib
中的新 Path.glob
似乎与旧 glob.glob
的行为不同。
In [1]: from pathlib import Path
In [2]: from glob import glob
In [3]: glob('webroot/*/')
Out[3]: ['webroot/2017-06-07/']
In [4]: list(Path().glob('webroot/*/'))
Out[4]:
[PosixPath('webroot/.keep'),
PosixPath('webroot/2017-06-07'),
PosixPath('webroot/matches.2017-06-07.json')]
这是设计使然,我没有遇到过一些兼容性问题吗?有没有办法阻止它这样做?
现在我将解决这个问题:
[path for path in Path().glob('webroot/*/') if path.is_dir()]
有一个关于此的未解决的错误:
尚未解决。
您的解决方法看起来不错,但如果您不介意还包括 'webroot' 目录本身,您可能更喜欢使用 **
glob:
>>> list(Path('webroot').glob('**'))
[PosixPath('webroot'), PosixPath('webroot/2017-06-07')]