Using the glob module in Python to find file paths _ basic knowledge

Source: Internet
Author: User
Tags glob
This article mainly introduces how to use the glob module in Python to find the file path. It is very simple to use the iglob method in the module, if you need it, refer to the glob module, which is one of the simplest modules with very little content. You can use it to find the path name of a file that complies with specific rules. It is similar to searching files in windows. Only three matching characters are used for searching a file: "*", "?", "[]"." * "Matches 0 or multiple characters ;"?" Match a single character. "[]" matches characters in the specified range, for example, [0-9] matches numbers.
Glob. glob

Returns the list of all matched file paths. It has only one pathname parameter and defines the file path matching rule. Here it can be an absolute path or a relative path. The following is an example of using glob. glob:

Import glob # obtain print glob for all images in the specified directory. glob (r "E:/Picture /*/*. jpg ") # obtain all contents in the parent directory. print glob in The py file. glob (R '.. /*. py') # Relative Path
Glob. iglob

Obtain a programmable object, which can be used to obtain matching file path names one by one. The difference with glob. glob () Is that glob. glob obtains all matching paths at the same time, while glob. iglob obtains only one matching path at a time. This is a bit similar to DataSet and DataReader used to operate databases in. NET. The following is a simple example:

Import glob # f = glob. iglob (R' ../*. py') print f #

For py in f: print py


It's so easy, isn't it?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.