[Python-checkins] cpython (merge 3.3 -> default): Issue #16695: Document how glob handles filenames starting with a dot

petri.lehtinen python-checkins at python.org
Sat Feb 23 19:58:25 CET 2013


http://hg.python.org/cpython/rev/3fd9970d9e65
changeset:   82359:3fd9970d9e65
parent:      82355:6b0ca4cb7e4e
parent:      82358:3e8b29512b2e
user:        Petri Lehtinen <petri at digip.org>
date:        Sat Feb 23 19:56:15 2013 +0100
summary:
  Issue #16695: Document how glob handles filenames starting with a dot

files:
  Doc/library/glob.rst |  15 +++++++++++++--
  Lib/glob.py          |  10 ++++++++--
  Misc/NEWS            |   3 +++
  3 files changed, 24 insertions(+), 4 deletions(-)


diff --git a/Doc/library/glob.rst b/Doc/library/glob.rst
--- a/Doc/library/glob.rst
+++ b/Doc/library/glob.rst
@@ -16,8 +16,10 @@
 ``*``, ``?``, and character ranges expressed with ``[]`` will be correctly
 matched.  This is done by using the :func:`os.listdir` and
 :func:`fnmatch.fnmatch` functions in concert, and not by actually invoking a
-subshell.  (For tilde and shell variable expansion, use
-:func:`os.path.expanduser` and :func:`os.path.expandvars`.)
+subshell.  Note that unlike :func:`fnmatch.fnmatch`, :mod:`glob` treats
+filenames beginning with a dot (``.``) as special cases.  (For tilde and shell
+variable expansion, use :func:`os.path.expanduser` and
+:func:`os.path.expandvars`.)
 
 For a literal match, wrap the meta-characters in brackets.
 For example, ``'[?]'`` matches the character ``'?'``.
@@ -51,6 +53,15 @@
    >>> glob.glob('?.gif')
    ['1.gif']
 
+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::
 
diff --git a/Lib/glob.py b/Lib/glob.py
--- a/Lib/glob.py
+++ b/Lib/glob.py
@@ -9,7 +9,10 @@
 def glob(pathname):
     """Return a list of paths matching a pathname pattern.
 
-    The pattern may contain simple shell-style wildcards a la fnmatch.
+    The pattern may contain simple shell-style wildcards a la
+    fnmatch. However, unlike fnmatch, filenames starting with a
+    dot are special cases that are not matched by '*' and '?'
+    patterns.
 
     """
     return list(iglob(pathname))
@@ -17,7 +20,10 @@
 def iglob(pathname):
     """Return an iterator which yields the paths matching a pathname pattern.
 
-    The pattern may contain simple shell-style wildcards a la fnmatch.
+    The pattern may contain simple shell-style wildcards a la
+    fnmatch. However, unlike fnmatch, filenames starting with a
+    dot are special cases that are not matched by '*' and '?'
+    patterns.
 
     """
     if not has_magic(pathname):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -1012,6 +1012,9 @@
 Documentation
 -------------
 
+- Issue #16695: Document how glob handles filenames starting with a
+  dot. Initial patch by Jyrki Pulliainen.
+
 - Issue #8890: Stop advertising an insecure practice by replacing uses
   of the /tmp directory with better alternatives in the documentation.
   Patch by Geoff Wilson.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list