/Doc/library/glob.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 54 lines · 36 code · 18 blank · 0 comment · 0 complexity · 1b69cfffcf3a14a216b024554bee6eea MD5 · raw file

  1. :mod:`glob` --- Unix style pathname pattern expansion
  2. =====================================================
  3. .. module:: glob
  4. :synopsis: Unix shell style pathname pattern expansion.
  5. .. index:: single: filenames; pathname expansion
  6. The :mod:`glob` module finds all the pathnames matching a specified pattern
  7. according to the rules used by the Unix shell. No tilde expansion is done, but
  8. ``*``, ``?``, and character ranges expressed with ``[]`` will be correctly
  9. matched. This is done by using the :func:`os.listdir` and
  10. :func:`fnmatch.fnmatch` functions in concert, and not by actually invoking a
  11. subshell. (For tilde and shell variable expansion, use
  12. :func:`os.path.expanduser` and :func:`os.path.expandvars`.)
  13. .. function:: glob(pathname)
  14. Return a possibly-empty list of path names that match *pathname*, which must be
  15. a string containing a path specification. *pathname* can be either absolute
  16. (like :file:`/usr/src/Python-1.5/Makefile`) or relative (like
  17. :file:`../../Tools/\*/\*.gif`), and can contain shell-style wildcards. Broken
  18. symlinks are included in the results (as in the shell).
  19. .. function:: iglob(pathname)
  20. Return an :term:`iterator` which yields the same values as :func:`glob`
  21. without actually storing them all simultaneously.
  22. .. versionadded:: 2.5
  23. For example, consider a directory containing only the following files:
  24. :file:`1.gif`, :file:`2.txt`, and :file:`card.gif`. :func:`glob` will produce
  25. the following results. Notice how any leading components of the path are
  26. preserved. ::
  27. >>> import glob
  28. >>> glob.glob('./[0-9].*')
  29. ['./1.gif', './2.txt']
  30. >>> glob.glob('*.gif')
  31. ['1.gif', 'card.gif']
  32. >>> glob.glob('?.gif')
  33. ['1.gif']
  34. .. seealso::
  35. Module :mod:`fnmatch`
  36. Shell-style filename (not path) expansion