METADATA 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. Metadata-Version: 2.0
  2. Name: scandir
  3. Version: 1.5
  4. Summary: scandir, a better directory iterator and faster os.walk()
  5. Home-page: https://github.com/benhoyt/scandir
  6. Author: Ben Hoyt
  7. Author-email: benhoyt@gmail.com
  8. License: New BSD License
  9. Platform: UNKNOWN
  10. Classifier: Development Status :: 5 - Production/Stable
  11. Classifier: Intended Audience :: Developers
  12. Classifier: Operating System :: OS Independent
  13. Classifier: License :: OSI Approved :: BSD License
  14. Classifier: Programming Language :: Python
  15. Classifier: Topic :: System :: Filesystems
  16. Classifier: Topic :: System :: Operating System
  17. Classifier: Programming Language :: Python
  18. Classifier: Programming Language :: Python :: 2
  19. Classifier: Programming Language :: Python :: 2.6
  20. Classifier: Programming Language :: Python :: 2.7
  21. Classifier: Programming Language :: Python :: 3
  22. Classifier: Programming Language :: Python :: 3.2
  23. Classifier: Programming Language :: Python :: 3.3
  24. Classifier: Programming Language :: Python :: 3.4
  25. Classifier: Programming Language :: Python :: 3.5
  26. Classifier: Programming Language :: Python :: 3.6
  27. Classifier: Programming Language :: Python :: Implementation :: CPython
  28. scandir, a better directory iterator and faster os.walk()
  29. =========================================================
  30. ``scandir()`` is a directory iteration function like ``os.listdir()``,
  31. except that instead of returning a list of bare filenames, it yields
  32. ``DirEntry`` objects that include file type and stat information along
  33. with the name. Using ``scandir()`` increases the speed of ``os.walk()``
  34. by 2-20 times (depending on the platform and file system) by avoiding
  35. unnecessary calls to ``os.stat()`` in most cases.
  36. Now included in a Python near you!
  37. ----------------------------------
  38. ``scandir`` has been included in the Python 3.5 standard library as
  39. ``os.scandir()``, and the related performance improvements to
  40. ``os.walk()`` have also been included. So if you're lucky enough to be
  41. using Python 3.5 (release date September 13, 2015) you get the benefit
  42. immediately, otherwise just
  43. `download this module from PyPI <https://pypi.python.org/pypi/scandir>`_,
  44. install it with ``pip install scandir``, and then do something like
  45. this in your code::
  46. # Use the built-in version of scandir/walk if possible, otherwise
  47. # use the scandir module version
  48. try:
  49. from os import scandir, walk
  50. except ImportError:
  51. from scandir import scandir, walk
  52. `PEP 471 <https://www.python.org/dev/peps/pep-0471/>`_, which is the
  53. PEP that proposes including ``scandir`` in the Python standard library,
  54. was `accepted <https://mail.python.org/pipermail/python-dev/2014-July/135561.html>`_
  55. in July 2014 by Victor Stinner, the BDFL-delegate for the PEP.
  56. This ``scandir`` module is intended to work on Python 2.6+ and Python
  57. 3.2+ (and it has been tested on those versions).
  58. Background
  59. ----------
  60. Python's built-in ``os.walk()`` is significantly slower than it needs to be,
  61. because -- in addition to calling ``listdir()`` on each directory -- it calls
  62. ``stat()`` on each file to determine whether the filename is a directory or not.
  63. But both ``FindFirstFile`` / ``FindNextFile`` on Windows and ``readdir`` on Linux/OS
  64. X already tell you whether the files returned are directories or not, so
  65. no further ``stat`` system calls are needed. In short, you can reduce the number
  66. of system calls from about 2N to N, where N is the total number of files and
  67. directories in the tree.
  68. In practice, removing all those extra system calls makes ``os.walk()`` about
  69. **7-50 times as fast on Windows, and about 3-10 times as fast on Linux and Mac OS
  70. X.** So we're not talking about micro-optimizations. See more benchmarks
  71. in the "Benchmarks" section below.
  72. Somewhat relatedly, many people have also asked for a version of
  73. ``os.listdir()`` that yields filenames as it iterates instead of returning them
  74. as one big list. This improves memory efficiency for iterating very large
  75. directories.
  76. So as well as a faster ``walk()``, scandir adds a new ``scandir()`` function.
  77. They're pretty easy to use, but see "The API" below for the full docs.
  78. Benchmarks
  79. ----------
  80. Below are results showing how many times as fast ``scandir.walk()`` is than
  81. ``os.walk()`` on various systems, found by running ``benchmark.py`` with no
  82. arguments:
  83. ==================== ============== =============
  84. System version Python version Times as fast
  85. ==================== ============== =============
  86. Windows 7 64-bit 2.7.7 64-bit 10.4
  87. Windows 7 64-bit SSD 2.7.7 64-bit 10.3
  88. Windows 7 64-bit NFS 2.7.6 64-bit 36.8
  89. Windows 7 64-bit SSD 3.4.1 64-bit 9.9
  90. Windows 7 64-bit SSD 3.5.0 64-bit 9.5
  91. CentOS 6.2 64-bit 2.6.6 64-bit 3.9
  92. Ubuntu 14.04 64-bit 2.7.6 64-bit 5.8
  93. Mac OS X 10.9.3 2.7.5 64-bit 3.8
  94. ==================== ============== =============
  95. All of the above tests were done using the fast C version of scandir
  96. (source code in ``_scandir.c``).
  97. Note that the gains are less than the above on smaller directories and greater
  98. on larger directories. This is why ``benchmark.py`` creates a test directory
  99. tree with a standardized size.
  100. The API
  101. -------
  102. walk()
  103. ~~~~~~
  104. The API for ``scandir.walk()`` is exactly the same as ``os.walk()``, so just
  105. `read the Python docs <https://docs.python.org/3.5/library/os.html#os.walk>`_.
  106. scandir()
  107. ~~~~~~~~~
  108. The full docs for ``scandir()`` and the ``DirEntry`` objects it yields are
  109. available in the `Python documentation here <https://docs.python.org/3.5/library/os.html#os.scandir>`_.
  110. But below is a brief summary as well.
  111. scandir(path='.') -> iterator of DirEntry objects for given path
  112. Like ``listdir``, ``scandir`` calls the operating system's directory
  113. iteration system calls to get the names of the files in the given
  114. ``path``, but it's different from ``listdir`` in two ways:
  115. * Instead of returning bare filename strings, it returns lightweight
  116. ``DirEntry`` objects that hold the filename string and provide
  117. simple methods that allow access to the additional data the
  118. operating system may have returned.
  119. * It returns a generator instead of a list, so that ``scandir`` acts
  120. as a true iterator instead of returning the full list immediately.
  121. ``scandir()`` yields a ``DirEntry`` object for each file and
  122. sub-directory in ``path``. Just like ``listdir``, the ``'.'``
  123. and ``'..'`` pseudo-directories are skipped, and the entries are
  124. yielded in system-dependent order. Each ``DirEntry`` object has the
  125. following attributes and methods:
  126. * ``name``: the entry's filename, relative to the scandir ``path``
  127. argument (corresponds to the return values of ``os.listdir``)
  128. * ``path``: the entry's full path name (not necessarily an absolute
  129. path) -- the equivalent of ``os.path.join(scandir_path, entry.name)``
  130. * ``is_dir(*, follow_symlinks=True)``: similar to
  131. ``pathlib.Path.is_dir()``, but the return value is cached on the
  132. ``DirEntry`` object; doesn't require a system call in most cases;
  133. don't follow symbolic links if ``follow_symlinks`` is False
  134. * ``is_file(*, follow_symlinks=True)``: similar to
  135. ``pathlib.Path.is_file()``, but the return value is cached on the
  136. ``DirEntry`` object; doesn't require a system call in most cases;
  137. don't follow symbolic links if ``follow_symlinks`` is False
  138. * ``is_symlink()``: similar to ``pathlib.Path.is_symlink()``, but the
  139. return value is cached on the ``DirEntry`` object; doesn't require a
  140. system call in most cases
  141. * ``stat(*, follow_symlinks=True)``: like ``os.stat()``, but the
  142. return value is cached on the ``DirEntry`` object; does not require a
  143. system call on Windows (except for symlinks); don't follow symbolic links
  144. (like ``os.lstat()``) if ``follow_symlinks`` is False
  145. * ``inode()``: return the inode number of the entry; the return value
  146. is cached on the ``DirEntry`` object
  147. Here's a very simple example of ``scandir()`` showing use of the
  148. ``DirEntry.name`` attribute and the ``DirEntry.is_dir()`` method::
  149. def subdirs(path):
  150. """Yield directory names not starting with '.' under given path."""
  151. for entry in os.scandir(path):
  152. if not entry.name.startswith('.') and entry.is_dir():
  153. yield entry.name
  154. This ``subdirs()`` function will be significantly faster with scandir
  155. than ``os.listdir()`` and ``os.path.isdir()`` on both Windows and POSIX
  156. systems, especially on medium-sized or large directories.
  157. Further reading
  158. ---------------
  159. * `The Python docs for scandir <https://docs.python.org/3.5/library/os.html#os.scandir>`_
  160. * `PEP 471 <https://www.python.org/dev/peps/pep-0471/>`_, the
  161. (now-accepted) Python Enhancement Proposal that proposed adding
  162. ``scandir`` to the standard library -- a lot of details here,
  163. including rejected ideas and previous discussion
  164. Flames, comments, bug reports
  165. -----------------------------
  166. Please send flames, comments, and questions about scandir to Ben Hoyt:
  167. http://benhoyt.com/
  168. File bug reports for the version in the Python 3.5 standard library
  169. `here <https://docs.python.org/3.5/bugs.html>`_, or file bug reports
  170. or feature requests for this module at the GitHub project page:
  171. https://github.com/benhoyt/scandir