How to use the Glob module in Python to find a file path _ basics

Source: Internet
Author: User
Tags glob in python

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; "?" Matches 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:

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:

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?

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.