>>> 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 โ€บ 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.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ pathlib.html
pathlib โ€” Object-oriented filesystem paths โ€” Python 3.15.2 documentation
Changed in version 3.13: Globbing with a pattern that ends with โ€œ**โ€ returns both files and directories. In previous versions, only directories were returned. In Path.glob() and rglob(), a trailing slash may be added to the pattern to match only directories.
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
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
๐ŸŒ r/learnpython
6
6
October 7, 2024
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
๐ŸŒ r/commandline
6
1
May 2, 2024
`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
๐ŸŒ r/golang
5
1
February 22, 2024
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ stdlib โ€บ glob
glob | Python Standard Library โ€“ Real Python
The Python glob module provides tools to find path names matching specified patterns that follow Unix shell rules.
๐ŸŒ
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).
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ glob-in-python
Glob in Python Explained | Built In
Glob is a Python module thatโ€™s used to search for file path names that match a specific pattern.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
How to Use glob() in Python | note.nkmk.me
July 28, 2023 - In Python, the glob module allows you to get a list or an iterator of file and directory paths that satisfy certain conditions using special characters like the wildcard *. glob โ€” Unix style pathname ...
Find elsewhere
๐ŸŒ
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')]
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python glob: filename pattern matching
Python Glob: Filename Pattern Matching โ€“ PYnative
June 17, 2021 - Python glob.glob() method returns a list of files or folders that matches the path specified in the pathname argument.
๐ŸŒ
TestDriven.io
testdriven.io โ€บ tips โ€บ 01895f3a-6f68-4790-b4cd-7483d106e63b
Tips and Tricks - Filename pattern matching in Python with pathlib.Path.glob() | TestDriven.io
You can use pathlib.Path.glob() to list all files matching a given pattern. For example, you can list names of all txt files located inside the same directory as your Python script๐Ÿ‘‡
๐ŸŒ
Runebook.dev
runebook.dev โ€บ en โ€บ docs โ€บ python โ€บ library โ€บ pathlib โ€บ pathlib.Path.glob
The Path to Discovery: Troubleshooting Python's Path.glob()
The glob() method is part of Python's pathlib module, which offers an object-oriented approach to filesystem paths. It lets you search for paths (files and directories) that match a shell-style wildcard pattern within the directory represented ...
๐ŸŒ
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.
๐ŸŒ
PyTutorial
pytutorial.com โ€บ python-pathlib-glob-explained
PyTutorial | Python Pathlib glob() Explained
March 19, 2025 - The pathlib.glob() method is a powerful tool for file pattern matching. It simplifies working with files and directories in Python.