Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the glob function of Python module

Shulou Source: shulou.com Published: 2022-06-02 22:49:36 09月29日 Update

Editor to share with you how to use the glob function of the Python module. I hope you will get something after reading this article. Let's discuss it together.

Outline of this article

The glob module is also an important module in the Python standard library, which is mainly used to find directories and files that meet specific rules, and return the search results to a list. The main reason for using this module is that the module supports several special regular wildcards, which is convenient to use, which will be explained in detail below.

Support for 4 commonly used wildcards

Using the glob module, we can quickly find the directories and files we want because it supports *, *,?, and [] wildcards, so what exactly do they mean?

*: match 0 or more characters

* *: match all files, directories, subdirectories and files in subdirectories (added in version 3.5)

?: generation matches a character

[]: match characters within the specified range, such as [0-9] match numbers, [amurz] match lowercase letters

Note: the usage of these three wildcards will be operated by everyone when talking about functions; the three main functions in the glob library

In fact, the glob library is very simple, there are only three main functions for us to use, they are glob (), iglob (), and escape () functions, so it is very easy to learn.

Glob.glob (): returns the path of all files that match the criteria

Glob.iglob (): returns an iterator object that needs to loop through each element and get the path of all files that match the criteria.

Glob.escape (): escape can ignore all special characters, such as asterisks, question marks, and square brackets, which are of little use.

Recursive=False: represents a recursive call, used with the special wildcard "* *". The default is False,False, which means no recursive call, and True means recursive call.

1) glob () function path2 = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_a\ [0-9] .png" glob.glob (path2) path3 = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_a\ [0-9a-z]. *" glob.glob (path3)

The results are as follows:

2) iglob () function path2 = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_a\ [0-9] .png" a = glob.iglob (path2) for i in a: print (I)

The results are as follows:

3) escape () function

By comparing the following two lines of code, you can see that the escape () function simply lets * express its original meaning rather than having the function of a wildcard.

Glob.glob ('tweets') glob.escape ('tweets')

The results are as follows:

After reading this article, I believe you have a certain understanding of "how to use the glob function of Python module". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!

Tags: Function module file wildcard character result special that is directory Huang Wei recursion support subdirectory meaning condition article path important three Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MySQL Linux Shulou Information Microsoft