[Python-checkins] peps: PEP 471: update by Ben Hoyt, simpler example

victor.stinner python-checkins at python.org
Sat Jul 19 18:52:15 CEST 2014


http://hg.python.org/peps/rev/8be4c830ea27
changeset:   5501:8be4c830ea27
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sat Jul 19 18:50:09 2014 +0200
summary:
  PEP 471: update by Ben Hoyt, simpler example

files:
  pep-0471.txt |  25 +++++++++++--------------
  1 files changed, 11 insertions(+), 14 deletions(-)


diff --git a/pep-0471.txt b/pep-0471.txt
--- a/pep-0471.txt
+++ b/pep-0471.txt
@@ -8,7 +8,7 @@
 Content-Type: text/x-rst
 Created: 30-May-2014
 Python-Version: 3.5
-Post-History: 27-Jun-2014, 8-Jul-2014, 14-Jul-2014, 18-Jul-2014
+Post-History: 27-Jun-2014, 8-Jul-2014, 14-Jul-2014
 
 
 Abstract
@@ -165,21 +165,18 @@
 Examples
 ========
 
-Below is a good usage pattern for ``scandir``. This is in fact almost
-exactly how the scandir module's faster ``os.walk()`` implementation
-uses it::
+First, a very simple example of ``scandir()`` showing use of the
+``DirEntry.name`` attribute and the ``DirEntry.is_dir()`` method::
 
-    dirs = []
-    non_dirs = []
-    for entry in os.scandir(directory):
-        if entry.is_dir():
-            dirs.append(entry)
-        else:
-            non_dirs.append(entry)
+    def subdirs(path):
+        """Yield directory names not starting with '.' under given path."""
+        for entry in os.scandir(path):
+            if not entry.name.startswith('.') and entry.is_dir():
+                yield entry.name
 
-The above ``os.walk()``-like code will be significantly faster with
-scandir than ``os.listdir()`` and ``os.path.isdir()`` on both Windows
-and POSIX systems.
+This ``subdirs()`` function will be significantly faster with scandir
+than ``os.listdir()`` and ``os.path.isdir()`` on both Windows and POSIX
+systems, especially on medium-sized or large directories.
 
 Or, for getting the total size of files in a directory tree, showing
 use of the ``DirEntry.stat()`` method and ``DirEntry.path``

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


More information about the Python-checkins mailing list