Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the fnmatch module

Shulou Source: shulou.com Published: 2022-06-02 05:54:05 09月28日 Update

Today, I will talk to you about how to use the fnmatch module, many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

The use of fnmatch module

The main function of this module is to match the file name and the unix shell style used by the matching pattern.

Fnmatch is relatively simple. The four methods are: fnmatch,fnmatchcase,filter,translate.

1.fnmatch

Fnmatch.fnmatch (filename, pattern)

Test filename for compliance with pattern.

Import fnmatch

Import os

Def run ():

For file in os.listdir ('.'): # os.listdir returns a list of files or folder names contained in the specified folder

If fnmatch.fnmatch (file,'* .py'): # determines whether there is a file with the suffix .py. * means that the file name length format is not limited.

Print (file)

If _ _ name__ = ='_ _ main__':

Run ()

2.fnmatchcase

Fnmatch.fnmatchcase (filename, pattern)

Similar to fnmatch (), except that fnmatchcase forces case-sensitive matching, regardless of whether the file system is case-sensitive or not.

Print (fnmatch.fnmatchcase ("text.py", "text.*")) # True

Print (fnmatch.fnmatchcase ("Text.py", "text.*")) # False

Print (fnmatch.fnmatchcase ("Text.Py", "* .py")) # False

Print (fnmatch.fnmatchcase ("Text.Py", "* .Py")) # True

3.filter

Fnmatch.filter (names, pattern)

Implement the filtering or filtering of list special characters and return a list of characters that match the matching pattern, which has a similar effect

[n for n in names if fnmatch (n, pattern)]

Filelist= ["a.text", "b.jpg", "c.png", "d.py", 'e.textbook, "sss.py"]

Print (fnmatch.filter (filelist, "? .py")) # matches the .py file preceded by a character

Output: d.py

4.translate

Fnmatch.translate (pattern):

Translation pattern, fnmatch converts this global pattern into a regular, and then uses the re module to compare names and patterns. The translate () function is a public API used to convert global schemas to regular expressions.

Regex = fnmatch.translate ('[frecinct dpense drecinct drecinct] .txt') # convert [frecinct dpentry dpense dpense gpenh] .txt to regular matching pattern

Print ("regex", regex) # (? s: [fPeragne drecinct drecinct dleglegleglegleglegle h]\ .txt)\ Z

#\ Z: matches the end of the string. If there is a newline, it only matches to the ending string before the newline.

Reobj = re.compile (regex) # generate re matching module

Print (reobj.match ('f.txt')) # returns a matching object

#

After reading the above, do you have any further understanding of how to use the fnmatch module? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: File pattern module character content regular function global name string folder special representative function just name suffix size object file name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MariaDB vpn Docker Shulou Information