python Wildcard Character glob Module Use Detailed Explanation

  • 2021-11-01 04:07:11
  • OfStack

Wildcards are special symbols, mainly asterisks (*) and question marks (? ), used to obscure the search file, "*" can match any number of symbols, "?" Can match a single character.

When looking for folders, you can use it instead of one or more real characters; Wildcard characters can be used instead of one or more real characters when you don't know the real characters or need to match multiple target files that meet the criteria of 1.

English "globbing" means unified distribution. python defines glob () function in module glob, which realizes the function of matching directory contents. glob. glob () function accepts the general distribution mode as input and returns a list of all matching file names and pathnames, which is similar to os. listdir.

Common functions in the glob module:


glob(pathname, recursive=False) 

The first parameter pathname is the string to match. (This parameter should be prefixed with r as much as possible to avoid unnecessary errors.)

The second parameter represents a recursive call, which is used with the special wildcard character "**" 1 and defaults to False.

This function returns a string list of qualified paths. If the Windows system is used, the "\" symbol on the path will be automatically added with an escape symbol to "\\".


iglob(pathname, recursive=False) 

The parameters are the same as glob () 1.

Returns an iterator that does not save all matched paths at the same time, but gets the matching file pathnames one by one, and the result of traversing the iterator is the same as the result of calling glob () with the same parameters.

Wildcard characters supported by glob module:

通配符 功能
* 匹配0或多个字符
** 匹配所有文件、目录、子目录和子目录里的文件(3.5版本新增)
? 匹配1个字符,与正则表达式里的?不同
[exp] 匹配指定范围内的字符,如:[1-9]匹配1至9范围内的字符
[!exp] 匹配不在指定范围内的字符

Example use of glob. glob function


import glob
 
listglob = []
listglob = glob.glob(r"/home/xxx/picture/*.png")
listglob.sort()
print listglob
 
print '--------------------'
listglob = glob.glob(r"/home/xxx/picture/0?.png")
listglob.sort()
print listglob
 
print '--------------------'
listglob = glob.glob(r"/home/xxx/picture/0[0,1,2].png")
listglob.sort()
print listglob
 
print '--------------------'
listglob = glob.glob(r"/home/xxx/picture/0[0-3].png")
listglob.sort()
print listglob
 
print '--------------------'
listglob = glob.iglob(r"/home/xxx/picture/0[a-z].png")
print listglob
for item in listglob:
    print item

Supplement: Python glob () function is understood in seconds

Python glob()

The glob module is one of the simplest modules, with very little content. Use it to find file pathnames that meet specific rules.

It is similar to using file search under windows. Find a file using only 3 matches: '*', "?" "," [] "." * "matches any 0 or more characters;"? "Matches any single character;" [] "matches a specified range of characters, such as [0-9] matches numbers.

Case insensitive

Mismatch at the beginning of '.'

print(glob.glob(r' . ./*') )

All directories at level 1 above


>>> print(glob.glob("../*"))
['..\\Python37-32', '..\\Python38-32']

print(glob.glob(r' ./*') )

All directories at this level


>>> print(glob.glob("./*"))
['.\\DLLs', '.\\Doc', '.\\include', '.\\Lib', '.\\libs', '.\\LICENSE.txt', '.\\NEWS.txt', '.\\python.exe', '.\\python3.dll', '.\\python38.dll', '.\\pythonw.exe', '.\\Scripts', '.\\tcl', '.\\Tools', '.\\vcruntime140.dll']

print(glob.glob(r' ./ * . *') )

All documents at this level


print(glob.glob("./*.*"))
['.\\LICENSE.txt', '.\\NEWS.txt', '.\\python.exe', '.\\python3.dll', '.\\python38.dll', '.\\pythonw.exe', '.\\vcruntime140.dll']

print(glob.glob(r' ./ * . *') )

All dll at this level


>>> print(glob.glob("./*.dll"))
['.\\python3.dll', '.\\python38.dll', '.\\vcruntime140.dll']

print(glob.glob(r' C:/ * ') )

All directories of C disk


>>> print(glob.glob("C:/*"))
['C:/$360Section', 'C:/$Recycle.Bin', 'C:/360SANDBOX', 'C:/Boot', 'C:/bootmgr'.......]

print (glob. glob ("C:/[PB] [RO]"))

C all directories containing pr/po/br/bo


print(glob.glob("C:/*[PB][RO]*"))
['C:/360SANDBOX', 'C:/Boot', 'C:/bootmgr', 'C:/BOOTNXT', 'C:/BOOTSECT.BAK', 'C:/PO', 'C:/Program Files', 'C:/Program Files (x86)', 'C:/ProgramData']

print (glob. glob ("C:/p? O"))

All directories on the C disk that contain P_o


>>> print(glob.glob("C:/*P?O*"))
['C:/Program Files', 'C:/Program Files (x86)', 'C:/ProgramData']

print (glob. glob ("C://. txt"))

C disk two-level directory all txt


iglob(pathname, recursive=False) 
0

Related articles: