Ways to find file paths using the Glob module in Python

Source: Internet
Author: User
Tags glob in python

This article mainly introduces in Python uses the Glob module to look for the file path the method, uses the module the Iglob method to realize is very simple, needs the friend to be possible to refer to under

The Glob module is one of the simplest modules with very little content. Use it to find a file path name that matches a specific rule. It's about the same as using a file search under Windows. The lookup file uses only three matches: "*", "?", "[]". "*" matches 0 or more characters; "?" Match a single character; [] matches a specified range of characters, such as: [0-9] matches a number.

Glob.glob

Returns a list of all matching file paths. It has only one parameter pathname, defines the file path matching rule, here can be absolute path, also can be a relative path. The following are examples of using Glob.glob:

?

1 2 3 4 5 6 7 8 Import glob #获取指定目录下的所有图片 print Glob.glob (r "e:/picture/*/*.jpg") #获取上级目录的所有. py File Print Glob.glob (r). /*.py ') #相对路径

Glob.iglob

Gets a calendar object that can be used to get a matching file pathname. The difference from Glob.glob () is that Glob.glob gets all the matching paths at the same time, and Glob.iglob gets only one matching path at a time. It's kind of like. NET, the dataset and DataReader used in the Operation database. The following is a simple example:

?

1 2 3 4 5 6 7 8 9 10 Import glob #父目录中的. py file F = Glob.iglob (R ' ... /*.py ') Print F #<generator object Iglob at 0x00b9ff80> for py in f:print py

It ' s so easy, is ' 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: info-contact@alibabacloud.com 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.