glob
--- Unix 形式のパス名のパターン展開¶
ソースコード: Lib/glob.py
glob
モジュールは Unix シェルで使われているルールに従い指定されたパターンに一致するすべてのパス名を見つけ出します。返される結果の順序は不定です。チルダ展開は行われませんが、*
, ?
, および []
で表現される文字範囲については正しくマッチされます。これは、関数 os.scandir()
および fnmatch.fnmatch()
を使用して行われており、実際にサブシェルを呼び出しているわけではありません。
Note that files beginning with a dot (.
) can only be matched by
patterns that also start with a dot,
unlike fnmatch.fnmatch()
or pathlib.Path.glob()
.
(For tilde and shell variable expansion, use os.path.expanduser()
and
os.path.expandvars()
.)
リテラルにマッチさせるには、メタ文字を括弧に入れてください。例えば、'[?]'
は文字 '?'
にマッチします。
参考
pathlib
モジュールは高水準のパスオブジェクトを提供します。
- glob.glob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False)¶
Return a possibly empty list of path names that match pathname, which must be a 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. Broken symlinks are included in the results (as in the shell). Whether or not the results are sorted depends on the file system. If a file that satisfies conditions is removed or added during the call of this function, whether a path name for that file be included is unspecified.If root_dir is not
None
, it should be a path-like object specifying the root directory for searching. It has the same effect onglob()
as changing the current directory before calling it. If pathname is relative, the result will contain paths relative to root_dir.この関数は dir_fd パラメタで ディレクトリ記述子への相対パス をサポートしています。
recursive が真の場合、パターン "
**
" はあらゆるファイルや0個以上のディレクトリ、サブディレクトリおよびディレクトリへのシンボリックリンクにマッチします。パターンの末尾がos.sep
またはos.altsep
の場合、ファイルは一致しません。If include_hidden is true, "
**
" pattern will match hidden directories.引数
pathname
,recursive
を指定して 監査イベントglob.glob
を送出します。引数
pathname
,recursive
,root_dir
,dir_fd
を指定して 監査イベントglob.glob/2
を送出します。注釈
パターン "
**
" を大きなディレクトリツリーで使用するととてつもなく時間がかかるかもしれません。バージョン 3.5 で変更: "
**
" を使った再帰的な glob がサポートされました。バージョン 3.10 で変更: root_dir と dir_fd 引数が追加されました。
バージョン 3.11 で変更: include_hidden パラメータが追加されました。
- glob.iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False)¶
実際には一度にすべてを格納せずに、
glob()
と同じ値を順に生成する イテレーター を返します。引数
pathname
,recursive
を指定して 監査イベントglob.glob
を送出します。引数
pathname
,recursive
,root_dir
,dir_fd
を指定して 監査イベントglob.glob/2
を送出します。バージョン 3.5 で変更: "
**
" を使った再帰的な glob がサポートされました。バージョン 3.10 で変更: root_dir と dir_fd 引数が追加されました。
バージョン 3.11 で変更: include_hidden パラメータが追加されました。
- glob.escape(pathname)¶
すべての特殊文字 (
'?'
、'*'
、'['
) をエスケープします。特殊文字を含んでいる可能性のある任意のリテラル文字列をマッチさせたいときに便利です。drive/UNC sharepoints の特殊文字はエスケープされません。たとえば Windows ではescape('//?/c:/Quo vadis?.txt')
は'//?/c:/Quo vadis[?].txt'
を返します。バージョン 3.4 で追加.
たとえば、次の 3 個のファイル 1.gif
, 2.txt
, card.gif
と、ファイル 3.txt
だけを含んだサブディレクトリ sub
があった場合、glob()
は以下の結果を返します。パスに接頭する要素がどう維持されるかに注意してください。:
>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
>>> glob.glob('**/*.txt', recursive=True)
['2.txt', 'sub/3.txt']
>>> glob.glob('./**/', recursive=True)
['./', './sub/']
ディレクトリが .
で始まるファイルを含んでいる場合、デフォルトでそれらはマッチしません。例えば、 card.gif
と .card.gif
を含むディレクトリを考えてください:
>>> import glob
>>> glob.glob('*.gif')
['card.gif']
>>> glob.glob('.c*')
['.card.gif']
参考
fnmatch
モジュールシェル形式の (パスではない) ファイル名展開