Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Undo most changes, add "Examples" heading.
  • Loading branch information
barneygale committed Oct 13, 2023
commit 689fcd6351a3082d7ff7289360438dcd84b1f755
72 changes: 39 additions & 33 deletions Doc/library/glob.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ unlike :func:`fnmatch.fnmatch` or :func:`pathlib.Path.glob`.
For a literal match, wrap the meta-characters in brackets.
For example, ``'[?]'`` matches the character ``'?'``.

.. seealso::
The :mod:`fnmatch` module offers shell-style filename (not path) expansion.

.. seealso::
The :mod:`pathlib` module offers high-level path objects.
The :mod:`glob` module defines the following functions:


.. function:: glob(pathname, *, root_dir=None, dir_fd=None, recursive=False, \
Expand Down Expand Up @@ -72,34 +68,6 @@ For example, ``'[?]'`` matches the character ``'?'``.

If *include_hidden* is true, "``**``" pattern will match hidden directories.

For example, consider a directory containing the following files:
:file:`1.gif`, :file:`2.txt`, :file:`card.gif` and a subdirectory :file:`sub`
which contains only the file :file:`3.txt`. :func:`glob` will produce
the following results. Notice how any leading components of the path are
preserved. ::

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
>>> glob.glob('**/*.txt', recursive=True)
['2.txt', 'sub/3.txt']
>>> glob.glob('./**/', recursive=True)
['./', './sub/']

If the directory contains files starting with ``.`` they won't be matched by
default. For example, consider a directory containing :file:`card.gif` and
:file:`.card.gif`::

>>> import glob
>>> glob.glob('*.gif')
['card.gif']
>>> glob.glob('.c*')
['.card.gif']

.. audit-event:: glob.glob pathname,recursive glob.glob
.. audit-event:: glob.glob/2 pathname,recursive,root_dir,dir_fd glob.glob

Expand Down Expand Up @@ -145,3 +113,41 @@ For example, ``'[?]'`` matches the character ``'?'``.
``escape('//?/c:/Quo vadis?.txt')`` returns ``'//?/c:/Quo vadis[?].txt'``.

.. versionadded:: 3.4


Examples
--------

Consider a directory containing the following files:
:file:`1.gif`, :file:`2.txt`, :file:`card.gif` and a subdirectory :file:`sub`
which contains only the file :file:`3.txt`. :func:`glob` will produce
the following results. Notice how any leading components of the path are
preserved. ::

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
>>> glob.glob('**/*.txt', recursive=True)
['2.txt', 'sub/3.txt']
>>> glob.glob('./**/', recursive=True)
['./', './sub/']

If the directory contains files starting with ``.`` they won't be matched by
default. For example, consider a directory containing :file:`card.gif` and
:file:`.card.gif`::

>>> import glob
>>> glob.glob('*.gif')
['card.gif']
>>> glob.glob('.c*')
['.card.gif']

.. seealso::
The :mod:`fnmatch` module offers shell-style filename (not path) expansion.

.. seealso::
The :mod:`pathlib` module offers high-level path objects.