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 glob module

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

Share

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

This article shows you how to use the glob module, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Glob module

Function description: the glob module can use Unix shell-style wildcards to match files and folders in a specific format, similar to windows's file search function. Instead of calling a child shell to implement the search function, the glob module calls os.listdir () and fnmatch.fnmatch () internally. Check the fnmatch I wrote earlier.

The glob module contains the following three functions: glob,isglob and escape.

Glob (pathname, recursive=False)

The first parameter is the string to be matched. (try to precede the string with r)

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

The path format can be an absolute path / usr/src/Python-1.5/Makefile or a relative path.. / Tools//.gif.

After version 3.5, the glob function supports a special wildcard "*" that matches all files and directories in the specified path, including all files and directories in subdirectories. To use this wildcard, you must add the recursive=True parameter.

(using this wildcard in the case of a complex directory structure may lead to performance degradation and drag down the running of the entire program, so use it with caution! )

For example, the directory contains files: 1.gif, 2.txt, card.gif contains subdirectories, the sub,sub directory contains 3.txt.

> import glob

> > glob.glob ('. / [0-9]. *')

['. / 1.gifbacks,'. / 2.txt']

> glob.glob ('* .gif')

['1.giftings,' card.gif']

> glob.glob ('? .gif')

['1.gif']

> glob.glob ('* * / * .txt', recursive=True)

['2.txtbrush,' sub/3.txt']

> > glob.glob ('. / * * /', recursive=True)

[. /','. / sub/']

It is important to note that if the file name is preceded by something like this, card.gif and .card.gif, you need to handle wildcard prefix in the following way.

> import glob

> glob.glob ('* .gif')

['card.gif']

> glob.glob ('. * .gif')

['.card.gif']

Iglob (pathname, recursive=False)

The parameter is the same as glob ().

Returns an iterator, and the result of traversing the iterator is the same as that of glob ().

Escape (pathname)

Escape all special characters ('?' ,'* 'and' [']). This is useful if you want to match any string that may contain special characters. Special characters in the drive / UNC share point are not escaped, for example, on Windows systems, escape ('/? / c:/Quo vadis?.txt') returns'/ /? / c:/Quo vadis [?] .txt'. But these illegal characters cannot be created, so they are generally used for testing.

The above content is how to use the glob module, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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