Dawn Drescher
Dawn Drescher

Reputation: 957

How to prevent pathlib’s Path.glob from returning files when the glob ends in a slash?

The the new Path.glob from pathlib seems to behave differently from the old glob.glob when the glob pattern ends in a slash it seems.

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')]

Is that by design, some compatibility issue I haven’t encountered? And is there a way to stop it from doing that?

For now I’ll work around it with:

[path for path in Path().glob('webroot/*/') if path.is_dir()]

Upvotes: 3

Views: 1208

Answers (1)

wim
wim

Reputation: 363233

There's an open bug about this:

No resolution yet.

Your workaround looks fine, although if you don't mind also including the 'webroot' directory itself you may prefer using a ** glob:

>>> list(Path('webroot').glob('**'))
[PosixPath('webroot'), PosixPath('webroot/2017-06-07')]

Upvotes: 2

Related Questions