Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the fnmatch module

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report