>>> 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 โบ glob.html
glob โ Unix style pathname pattern expansion โ Python 3.15.2 documentation
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 correctly matched.
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
Get all files recursively that match a filter criteria
https://docs.python.org/3/library/pathlib.html#pathlib.Path.rglob then filter more if required. More on reddit.com
What's a reasonable behaviour of .. in glob in presence of symlinks?
I think it would depend on your quoting. As it stands with your example, I believe that the shell does the globbing before it reaches a single line of your code: $ mkdir test2 $ ln -s test2 test3 $ echo hello > abcd.txt $ echo --glob test3/../*.txt --glob test3/../abcd.txt $ GLOB="test3/../*.txt" $ echo --glob $GLOB --glob test3/../abcd.txt However, if you quote the variable expansion $ echo --glob "$GLOB" --glob test3/../*.txt it doesn't expand the glob. So if your program expects the shell to do the globbing before it runs, then the symlink resolution is irrelevant because the shell has already done it. But if you're quoting the parameter, your code is free to do whatever you want/document. That said, I'd treat the "symlnk/../" as explicitly specified and let it slide through even though it involves a symlink. I'd only consider the "don't process symlinks" at the leaf-ends of any globs. More on reddit.com
`filepath.Glob` not reading files when running tests
fortunatefaileur probably has it correct, but also check all the hidden files and directories that may match the directories and make sure there aren't any dangling symlinks. filepath.Walk doesn't like those and Glob may not either. I use emacs and it annoys several Go-based programs when it leaves those behind. More on reddit.com
Earth Data Science
earthdatascience.org โบ home
Use the OS and Glob Python Packages to Manipulate File Paths | Earth Data Science - Earth Lab
November 12, 2020 - Then, you will use the os and glob packages to access files and directories and to create lists of paths that you can parse to extract useful information from the file and directory names. You can use the function data.get_data() from the earthpy package to download data from online sources such as the Figshare.com data repository. Begin by importing the necessary packages: os, glob, and earthpy (using the alias et).
Georgefox
cs.georgefox.edu โบ hs_contest โบ docs โบ python-3.10.12-docs โบ library โบ glob.html
glob โ Unix style pathname pattern expansion โ Python 3.10.12 documentation
The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, although results are returned in arbitrary order. No tilde expansion is done, but *, ?, and character ranges expressed with [] will be correctly matched.
Michael Currin
michaelcurrin.github.io โบ dev-cheatsheets โบ cheatsheets โบ python โบ files-and-paths โบ glob.html
Glob | Dev Cheatsheets
glob1(dirname, pattern) # e.g. glob1("foo", "*.txt") # => [ 'a.txt', 'b.txt' ] ... Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname ยท can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools//.gif),
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
docs.python.org โบ 3.9 โบ library โบ glob.html
glob โ Unix style pathname pattern expansion โ Python 3.9.25 documentation
July 8, 2025 - The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, although results are returned in arbitrary order. No tilde expansion is done, but *, ?, and character ranges expressed with [] will be correctly matched.
YouTube
youtube.com โบ watch
Python Pathlib Glob Tutorial | Replace Glob Module with Pathlib - YouTube
SEO Description:Learn how to replace Pythonโs traditional glob module with the modern pathlib library. In this tutorial, weโll explore how Path.glob() makes ...
Published: August 28, 2025
GeeksforGeeks
geeksforgeeks.org โบ python โบ how-to-use-glob-function-to-find-files-recursively-in-python
How to use Glob() Function to Find Files Recursively in Python - GeeksforGeeks
October 3, 2025 - From Python 3.5 onwards, the glob module supports recursive searching using the ** pattern combined with the recursive=True argument. It's Key Points include: ** means โthis directory and all subdirectories, recursively.โ ยท You must set recursive=True for ** to be interpreted as recursive. Both glob.glob() (returns a list) and glob.iglob() (returns an iterator) support recursion. glob.glob(pathname, *, recursive=False) glob.iglob(pathname, *, recursive=False) pathname: The pattern string, which can include wildcards and directory paths.
Roadmap
roadmap.sh โบ python โบ glob
Python glob Module: File Pattern Matching Explained
March 3, 2026 - Below is an example of how to use glob to search for all matching text files: ... This returns a list of all the pathnames matching the .txt pattern in the current directory. So far, we have covered what the module is and the problem it solves. The next step is to apply it in real code and explore how pattern-based file matching fits into everyday Python projects.