>>> 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 Overflow
🌐
Python
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.
Discussions

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
🌐 r/learnpython
7
3
January 8, 2023
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
🌐 discuss.python.org
1
September 30, 2024
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
🌐 github.com
7
January 14, 2016
Globbing with glob vs. pathlib
Globbing with glob vs. pathlib#16 More on github.com
🌐 github.com
1
February 5, 2018
🌐
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')]
🌐
Python
docs.python.org › 3 › library › glob.html
glob — Unix style pathname pattern expansion
Source code: Lib/glob.py The glob module finds pathnames using pattern matching rules similar to the Unix shell. No tilde expansion is done, but*,?, and character ranges expressed with[] will be co...
🌐
Romain-clement
romain-clement.net › articles › python-pathlib-glob-patterns
Romain Clement - Python Pathlib complex glob patterns
September 18, 2023 - import itertools import pathlib mypath = pathlib.Path() patterns = ["*.jpg", "*.png"] matched = list( itertools.chain.from_iterable( mypath.glob(pattern) for pattern in patterns ) )
Find elsewhere
🌐
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.
🌐
DataCamp
datacamp.com › tutorial › comprehensive-tutorial-on-using-pathlib-in-python-for-file-system-manipulation
How to Use Python's Pathlib (with Examples) | DataCamp
May 22, 2024 - pathlib uses the built-in .glob() module to efficiently search for files matching a specific pattern in any directory. This module is very useful when processing files with similar names or extensions.
🌐
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):
🌐
Switowski
switowski.com › blog › pathlib
Pathlib for Path Manipulations - Sebastian Witowski
You no longer need the glob module to search for files matching a pattern, and you also don't need the os module to get the names of their directories. All this functionality can now be found in the pathlib module (of course, you can still use the os or glob modules, if you prefer).
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › pathlib-module-in-python
Pathlib module in Python - GeeksforGeeks
September 8, 2025 - The pathlib module in Python (introduced in version 3.4) provides an object-oriented way to work with filesystem paths.
🌐
Real Python
realpython.com › python-pathlib
Python's pathlib Module: Taming the File System – Real Python
January 11, 2025 - pathlib allows you to read, write, move, and delete files efficiently using methods. To get a list of file paths in a directory, you can use .iterdir(), .glob(), or .rglob().
🌐
GitHub
github.com › python › cpython › issues › 70303
pathlib.glob('**') returns only directories · Issue #70303 · python/cpython
January 14, 2016 - GH-70303: Make pathlib.Path.glob('**') return both files and directories #114684
Author   jitterman
🌐
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