0

I'm trying to iterate over all directories (not recursively) inside a given directory. I have this file structure:

debian/
dev-requirements.txt
MANIFEST.in
mypy.ini
pytest.ini
README.md
requirements.txt
resources/
reviewer/
setup.py*
tests/
tox.ini

If i use glob.glob, it's yielding the correct results:

>>> import glob
>>> import os
>>> glob.glob(f'*{os.path.sep}')
['reviewer/', 'debian/', 'resources/', 'tests/']

But with Path.glob it's yiedling different results for the same pattern:

>>> from pathlib import Path
>>> [e.name for e in Path().glob(f'*{os.path.sep}')]
['reviewer', 'debian', '.coveragerc', '.gitignore', '.mypy_cache', '.venv', 'dev-requirements.txt', 'MANIFEST.in', 'tox.ini', 'setup.py', '.git', 'mypy.ini', '.idea', '.tox', 'resources', '.pyreview.ini', 'README.md', 'pytest.ini', '.coverage', 'requirements.txt', '.pytest_cache', 'tests']

Shouldn't both operations be equivalent? I know there are other ways to do what I'm trying to do, that's not my question; I want to know why are both operations yielding different results for the same glob pattern.

EDIT: same question was answered here

odradek
  • 993
  • 7
  • 14
  • 1
    This question https://stackoverflow.com/questions/44420100/how-to-prevent-pathlib-s-path-glob-from-returning-files-when-the-glob-ends-in-a has an answer that links to this open bug : https://bugs.python.org/issue22276 – Thierry Lathuille May 04 '20 at 14:52
  • 1
    @ThierryLathuille, in [this issue#26096 Guido explained that consider it a feature](https://bugs.python.org/issue26096) – buran May 04 '20 at 14:55
  • Oooh, I see. So these differences between `glob` and `Path.glob` are intended. It would be nice to have them listed somewhere. Thanks very much. – odradek May 04 '20 at 15:09
  • @buran That discussion is about dot files. Looking further on the issue with the final '/', there is a pull request to correct that, see https://github.com/python/cpython/pull/10349#pullrequestreview-171866354 – Thierry Lathuille May 04 '20 at 15:12
  • @ThierryLathuille Indeed, closed as duplicate of the question you provided. Thanks very much. – odradek May 04 '20 at 15:14
  • @ThierryLathuille, it clearly refers to both "files and directories". Yes, Guido mention just "dor files", but I think it's clear that he refers to both files and directories that start with dot. The pull request you link is about ending separator, not about the difference between between `glob.glob` and `pathlib.Path.glob` with respect to hidden files and directories. – buran May 04 '20 at 15:33
  • @ThierryLathuille, now I see what you mean. – buran May 04 '20 at 15:34

0 Answers0