Python module-detailed explanation of glob module

Outline of this article

glob模块It is also an important module in the Python standard library, mainly used to find 符合特定规则directories and files, and return the search results to a list. The main reason for using this module is that there are 支持several special modules that are 正则通配符convenient to use. This will be explained in detail below.
Insert picture description here

Support 4 commonly used wildcards

Use glob module can quickly find directories and files we want, it is because it supports *, **, ?, [ ]three wildcards, then they are in the end what does that mean?

  • *: Match 0 or more characters;
  • **: Match all files, directories, subdirectories and files in subdirectories (new in version 3.5);
  • ?: Match one character;
  • []: Match characters in the specified range, such as [0-9] matches numbers, [az] matches lowercase letters;
  • 注意: The usage of these 3 wildcards will be explained together with everyone when we talk about functions;

The 3 main functions in the glob library

In fact glob library is very simple, only three major functions for our use, they are glob(), iglob(), escape()function, and therefore it is particularly easy to learn.

  • glob.glob(): Return the path of all files that meet the matching conditions;
  • glob.iglob(): Return an iterator object, you need to loop through to get each element, and get the path of all files that meet the matching conditions;
  • glob.escape(): Escape can ignore all special characters, that is, asterisks, question marks, brackets, which are not very useful;
  • recursive=False: Represents recursive call, “**”used together with special wildcards , the default is False, False means no recursive call, True means recursive call;

1) glob() function

path1 = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_a\[0-9].png"
glob.glob(path1)

path2 = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_a\[0-9a-z].*"
glob.glob(path2)

The results are as follows:
Insert picture description here

2) iglob() function

path1 = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_a\[0-9].png"
a = glob.iglob(path1)
for i in a:
    print(i)

The results are as follows:
Insert picture description here

3) escape() function

By comparing the two lines of code below, it can be seen that the escape() function *only expresses its original meaning and no longer has the role of wildcard.

glob.glob('t*')
glob.escape('t*')

The results are as follows:
Insert picture description here

to sum up

From the above description, we can know that the glob library does not actually have a lot of things, just remember 3个通配符it 3个函数. For us, the glob library was born to facilitate our search for files, so we have to master the glob() function, which is actually enough, the others know and can use it.

We also need to note that, os库, shutil库, glob库are complementary, we should be good play to their strengths and make full use of their advantages to help us to quickly manipulate files and folders.

Guess you like

Origin blog.csdn.net/weixin_41261833/article/details/108069945