>>> 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 - 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.
🌐
Python
docs.python.org › 3 › library › glob.html
glob — Unix style pathname pattern expansion
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.
Discussions

python - Loop over results from Path.glob() (Pathlib) - Stack Overflow
I'm struggling with the result of the Path.glob() method of the Pathlib module in Python 3.6. More on stackoverflow.com
🌐 stackoverflow.com
Speed up `pathlib.Path.glob()` by removing redundant regex matching
In #104512 we made pathlib.Path.glob() use a "walk-and-filter" strategy for expanding ** wildcards in patterns: when we encounter a ** segment, we immediately consume subsequent segments ... More on github.com
🌐 github.com
4
February 6, 2024
`Path.rglob` -> documentation does not specify what `pattern` is
I went to https://docs.python.org/3/library/pathlib.html#pathlib.Path.rglob and tried to find out what pattern actually is. While the current documentation links to Path.glob(), I would like to add a note similar you can find at https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob, ie More on github.com
🌐 github.com
6
January 17, 2023
pathlib .glob('*/') returns files as well as directories
BPO 33392 Nosy @emilyemorehouse, @robbuckley, @BrianMSheldon PRs #10349 Superseder bpo-22276: pathlib glob ignores trailing slash in pattern Note: these values reflect the state of the issue at the time it was migrated and might not refl... More on github.com
🌐 github.com
5
April 30, 2018
🌐
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 ) )
🌐
Scriptling
scriptling.dev › docs › libraries › extlib › glob
glob - Scriptling
import glob # Find all Python files in the current directory files = glob.glob("*.py") print(files) # ["script.py", "app.py", ...] # Find all text files recursively all_txt = glob.glob("**/*.txt") Find all pathnames matching a pattern.
Find elsewhere
🌐
Astral
docs.astral.sh › ruff › rules › glob
glob (PTH207) | Ruff
When possible, using Path object methods such as Path.glob() can improve readability over their low-level counterparts (e.g., glob.glob()).
🌐
Switowski
switowski.com › blog › pathlib
Pathlib for Path Manipulations
Even if you figured out how to make paths work on different operating systems, the functions you can use with file paths are a bit scattered around different modules. Sure, most of them live in the os.path module. But if you want to search for filenames matching a pattern, you must use the glob() function from the glob module.
🌐
Python Packaging
packaging.python.org › en › latest › specifications › glob-patterns
glob patterns - Python Packaging User Guide
MUST treat each value as a glob pattern, and MUST raise an error if the pattern contains invalid glob syntax. MUST raise an error if any individual user-specified pattern does not match at least one file. ... "..\LICENSE.MIT" # .. must not be used. # \ is an invalid path delimiter, / must be used.
🌐
GitHub
github.com › python › cpython › issues › 115060
Speed up `pathlib.Path.glob()` by removing redundant regex matching · Issue #115060 · python/cpython
February 6, 2024 - In #104512 we made pathlib.Path.glob() use a "walk-and-filter" strategy for expanding ** wildcards in patterns: when we encounter a ** segment, we immediately consume subsequent segments and use them to build a regex that is used to filter results.
Author   barneygale
🌐
Visual Studio Code
code.visualstudio.com › docs › editor › glob-patterns
Glob Patterns Reference
November 3, 2021 - In the settings, you must use **/example to match a folder named example in subfolder folder1/example in your workspace. In the Search view, the ** prefix is assumed. The glob patterns in these settings are always evaluated relative to the path of the workspace folder.
🌐
Medium
martinxpn.medium.com › glob-working-with-files-in-python-64-100-days-of-python-31e0916f046e
Glob — Working with Files in Python (64/100 Days of Python) | by Martin Mirakyan | Medium
April 10, 2023 - Glob — Working with Files in Python (64/100 Days of Python) Glob is a Python module that provides a convenient way to search for files that match a specified pattern. It allows you to use wildcard …
🌐
GitHub
github.com › python › cpython › issues › 101112
`Path.rglob` -> documentation does not specify what `pattern` is · Issue #101112 · python/cpython
January 17, 2023 - Documentation Today I came across Path.rglob in our codebase. I went to https://docs.python.org/3/library/pathlib.html#pathlib.Path.rglob and tried to find out what pattern actually is. While the current documentation links to Path.glob(...
Author   jugmac00
🌐
GitHub
github.com › python › cpython › issues › 77573
pathlib .glob('*/') returns files as well as directories · Issue #77573 · python/cpython
April 30, 2018 - pathlib .glob('*/') returns files as well as directories#77573 · Copy link · Assignees · Labels · type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error · robbuckleymannequin · opened · on Apr 30, 2018 · Issue body actions ·
Author   robbuckley
🌐
TetraScience
developers.tetrascience.com › docs › common-glob-pattern
Common Glob Patterns
Glob patterns specify sets of file names with wildcard characters. When you configure the Tetra File-Log Agent (FLA) File Watcher Service, you can use glob patterns to monitor specified paths and detect changes in either certain files or folders.
🌐
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 - Use glob to get customized lists of files or directories. Use various functions in the os package to manipulate file paths.
🌐
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.
🌐
Node.js
nodejs.org › api › fs.html
File system | Node.js v25.9.0 Documentation
Returns: <AsyncIterator> An AsyncIterator that yields the paths of files that match the pattern. import { glob } from 'node:fs/promises'; for await (const entry of glob('**/*.js')) console.log(entry); const { glob } = require('node:fs/promises'); (async () => { for await (const entry of glob('**/*.js')) console.log(entry); })(); copy