Movatterモバイル変換


[0]ホーム

URL:


Navigation

11.6.glob — Unix style pathname pattern expansion

Source code:Lib/glob.py


Theglob module finds all the pathnames matching a specified patternaccording to the rules used by the Unix shell. No tilde expansion is done, but*,?, and character ranges expressed with[] will be correctlymatched. This is done by using theos.listdir() andfnmatch.fnmatch() functions in concert, and not by actually invoking asubshell. Note that unlikefnmatch.fnmatch(),glob treatsfilenames beginning with a dot (.) as special cases. (For tilde and shellvariable expansion, useos.path.expanduser() andos.path.expandvars().)

For a literal match, wrap the meta-characters in brackets.For example,'[?]' matches the character'?'.

glob.glob(pathname)

Return a possibly-empty list of path names that matchpathname, which must bea string containing a path specification.pathname can be either absolute(like/usr/src/Python-1.5/Makefile) or relative (like../../Tools/*/*.gif), and can contain shell-style wildcards. Brokensymlinks are included in the results (as in the shell).

glob.iglob(pathname)

Return aniterator which yields the same values asglob()without actually storing them all simultaneously.

For example, consider a directory containing only the following files:1.gif,2.txt, andcard.gif.glob() will producethe following results. Notice how any leading components of the path arepreserved.

>>>importglob>>>glob.glob('./[0-9].*')['./1.gif', './2.txt']>>>glob.glob('*.gif')['1.gif', 'card.gif']>>>glob.glob('?.gif')['1.gif']

If the directory contains files starting with. they won’t be matched bydefault. For example, consider a directory containingcard.gif and.card.gif:

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

See also

Modulefnmatch
Shell-style filename (not path) expansion

Previous topic

11.5.tempfile — Generate temporary files and directories

Next topic

11.7.fnmatch — Unix filename pattern matching

This Page

Quick search

Enter search terms or a module, class or function name.

Navigation

©Copyright 1990-2017, Python Software Foundation.
The Python Software Foundation is a non-profit corporation.Please donate.
Last updated on Sep 19, 2017.Found a bug?
Created usingSphinx 1.2.

[8]ページ先頭

©2009-2025 Movatter.jp