>>> from pathlib import Path
>>>
>>> _dir = Path.cwd()
>>>
>>> files = _dir.glob('*.txt')
>>>
>>> type(files)
<class 'generator'>
Here, files is a generator, which can be read only once and then get exhausted. So, when you will try to read it second time, you won't have it.
>>> for i in files:
... print(i)
...
/home/ahsanul/test/hello1.txt
/home/ahsanul/test/hello2.txt
/home/ahsanul/test/hello3.txt
/home/ahsanul/test/b.txt
>>> # let's loop though for the 2nd time
...
>>> for i in files:
... print(i)
...
>>>
Answer from Ahasanul Haque on Stack OverflowPython
docs.python.org › 3 › library › pathlib.html
pathlib — Object-oriented filesystem paths
February 23, 2026 - Raises an auditing event pathlib.Path.glob with arguments self, pattern.
glob.glob vs Path.glob
glob.glob returns meager ol' strings whereas Path.glob returns you easy to work with path objects. More on reddit.com
Pathlib objects as glob template - Ideas - Discussions on Python.org
Currently, there’s no obvious way to use glob using the pathlib API (we cannot use shutil.glob as it does not support Google cloud, s3 buckets): path = 's3://home/*/documents/*.txt' path = s3_path.Path(path) files = list(path.glob()) # Not working Currently it require some ugly hack, like: ... More on discuss.python.org
pathlib.glob('**') returns only directories
BPO 26115 Nosy @pitrou Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state. Show more details GitHub fields: assignee = None closed... More on github.com
Globbing with glob vs. pathlib
Globbing with glob vs. pathlib#16 More on github.com
Videos
34:51
Python Tutorial: Pathlib - The Modern Way to Handle File Paths ...
11:18
Python Pathlib Glob Tutorial | Replace Glob Module with Pathlib ...
13:52
Pathlib makes file management EASY in Python - YouTube
11:53
Glob Module: File Searching in Python - YouTube
03:43
How to use glob() to find files recursively? - YouTube
Waylon Walker
waylonwalker.com › python-pathlib-glob
How I glob for Files in Python | Waylon Walker
March 17, 2022 - ❯ Path().glob("**/*.md") <generator object Path.glob at 0x7fa35adc4f90> ❯ list(Path().glob("**/*.md")) [ PosixPath('readme.md'), PosixPath('READMES.md'), PosixPath('README.md'), PosixPath('content/you.md'), PosixPath('content/me.md'), PosixPath('content/hello.md') ] ❯ list(Path().glob("**/*.py")) [PosixPath('setup.py'), PosixPath('content/hello.py')] ❯ list(Path().glob("*.md")) [PosixPath('readme.md'), PosixPath('READMES.md'), PosixPath('README.md')] ❯ list(Path().glob("*.py")) [PosixPath('setup.py')] ❯ list(Path().glob("**/*hello*")) [PosixPath('content/hello.py'), PosixPath('content/hello.md')] ❯ list(Path().glob("**/REA?ME.md")) [PosixPath('README.md')]
Reddit
reddit.com › r/learnpython › glob.glob vs path.glob
r/learnpython on Reddit: glob.glob vs Path.glob
January 8, 2023 -
For listing a directory contents why would I choose
Path('dirname').glob('*')instead of
glob.glob('dirname/*')Calmcode
calmcode.io › course › pathlib › glob
Calmcode - pathlib: Glob
You can iterate over files and folders that match a pattern by using the .glob() method on a path.
Python.org
discuss.python.org › ideas
Pathlib objects as glob template - Ideas - Discussions on Python.org
September 30, 2024 - Currently, there’s no obvious way to use glob using the pathlib API (we cannot use shutil.glob as it does not support Google cloud, s3 buckets): path = 's3://home/*/documents/*.txt' path = s3_path.Path(path) files = list(path.glob()) # Not working Currently it require some ugly hack, like: ...
Readthedocs
pathlib.readthedocs.io
pathlib — pathlib 1.0.1 documentation
>>> from pathlib import * >>> Path('.').exists() True >>> Path('setup.py').exists() True >>> Path('/etc').exists() True >>> Path('nonexistentfile').exists() False ... Glob the given pattern in the directory represented by this path, yielding all matching files (of any kind):
Astral
docs.astral.sh › ruff › rules › glob
glob (PTH207) | Ruff
import glob import os glob.glob(os.path.join("my_path", "requirements*.txt")) ... While using pathlib can improve the readability and type safety of your code, it can be less performant than the lower-level alternatives that work directly with strings, especially on older versions of Python.
GitHub
github.com › arogozhnikov › python3_with_pleasure › issues › 16
Globbing with glob vs. pathlib · Issue #16 · arogozhnikov/python3_with_pleasure
February 5, 2018 - It might be worth mentioning that glob.glob and pathlib.Path.glob work slightly differently. glob.glob treats dotfiles specially, while pathlib.Path.glob uses ** only to match directories, not files. It's easy to use pathlib.Path.glob wi...
Author gpoore
GitHub
facelessuser.github.io › wcmatch › pathlib
wcmatch.pathlib
Python's pathlib has logic to ignore . when used as a directory in both the file path and glob pattern. We do not alter how pathlib stores paths, but our implementation allows explicit use of . as a literal directory and will match accordingly. With that said, since pathlib normalizes paths ...
Roadmap
roadmap.sh › python › glob
Python glob Module: File Pattern Matching Explained
March 18, 2026 - import os # Current directory: ... in situations where consistency matters more, such as production systems. The pathlib module pairs nicely with glob because it uses an object-oriented approach....
Python
bugs.python.org › issue26115
Issue 26115: pathlib.glob('**') returns only directories - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/70303