glob
— Unix style pathname pattern expansion¶
Вихідний код: Lib/glob.py
The glob
module finds all the pathnames matching a specified pattern
according to the rules used by the Unix shell, although results are returned in
arbitrary order. No tilde expansion is done, but *
, ?
, and character
ranges expressed with []
will be correctly matched. This is done by using
the os.scandir()
and fnmatch.fnmatch()
functions in concert, and
not by actually invoking a subshell. Note that unlike fnmatch.fnmatch()
,
glob
treats filenames beginning with a dot (.
) as special cases.
(For tilde and shell variable expansion, use os.path.expanduser()
and
os.path.expandvars()
.)
Для буквального збігу заберіть метасимволи в дужки. Наприклад, ''[?]'
відповідає символу ''?'
.
Дивись також
Модуль pathlib
пропонує об’єкти шляху високого рівня.
-
glob.
glob
(pathname, *, recursive=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.Якщо recursive має значення true, шаблон «
**
» відповідатиме будь-яким файлам і нулю або більше каталогів, підкаталогів і символічних посилань на каталоги. Якщо за шаблоном йдеos.sep
абоos.altsep
, файли не збігатимуться.Викликає подію аудиту
glob.glob
з аргументамиpathname
,recursive
.Примітка
Використання шаблону «
**
» у великих деревах каталогів може зайняти надто багато часу.Змінено в версії 3.5: Підтримка рекурсивних глобусів з використанням «
**
».
-
glob.
iglob
(pathname, *, recursive=False)¶ Повертає iterator, який дає ті самі значення, що й
glob()
, фактично не зберігаючи їх усі одночасно.Викликає подію аудиту
glob.glob
з аргументамиpathname
,recursive
.
-
glob.
escape
(pathname)¶ Екранування всіх спеціальних символів (
'?'
,'*'
і'['
). Це корисно, якщо ви хочете зіставити довільний літеральний рядок, який може містити спеціальні символи. Спеціальні символи в точках доступу/UNC не екрануються, напр. у Windowsescape('//?/c:/Quo vadis?.txt')
повертає'//?/c:/Quo vadis[?].txt''
.Нове в версії 3.4.
For example, consider a directory containing the following files:
1.gif
, 2.txt
, card.gif
and a subdirectory sub
which contains only the file 3.txt
. glob()
will produce
the following results. Notice how any leading components of the path are
preserved.
>>> 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']
Дивись також
- Module
fnmatch
Shell-style filename (not path) expansion