Python glob Model

Source: Internet
Author: User
Tags glob

Note:

1. glob is a python file operation module. It can be used to find files that match your own purposes. It is similar to file search in windows and supports wildcard operations ,*,?, [] These three wildcards * represent 0 or multiple characters ,? Represents a character. [] matches a character in the specified range, for example, [0-9] matches a number.

Its main method is glob, which returns the list of all matched file paths, this method requires a parameter to specify the matched path string (this string can be an absolute or relative path). The returned file name only includes the file name in the current directory, does not include files in subfolders.

For example:

Glob. glob (r 'C: \ *. txt ')

Here, I will obtain all the TXT files under drive C.

Glob. glob (r 'e: \ PIC \ *. jpg ')

Obtains all JPG files in the specified directory.

Use relative path:

Glob. glob (R' ../*. py ')

2. iglob method:

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:
 
#. Py file in the parent directory
F = glob. iglob (R' ../*. py ')

Print F # <generator object iglob at 0x00b9ff80>

For py in F:
   Print py

 

Official notes:

Glob. Glob ( Pathname )
Return a possibly-empty list of path names that match Pathname, Which must be a string containing a Path Specification. PathnameCan be either absolute (like /Usr/src/Python-1.5/makefile) Or relative (like Http://www.cnblogs.com/tools/#/developer.gif), And can contain shell-style wildcards. Broken symlinks are encoded in the results (as in the shell ).
Glob. Iglob ( Pathname )

ReturnIteratorWhich yields the same valuesGlob ()Without actually storing them all simultaneously.

New in version 2.5.

For example, consider a directory containing only the following files:1. gif,2. txt, AndCard.gif.Glob ()Will produce the following results. Notice how any leading components of the path are preserved.

>>> import glob>>> glob.glob('./[0-9].*')['./1.gif', './2.txt']>>> glob.glob('*.gif')['1.gif', 'card.gif']>>> glob.glob('?.gif')['1.gif']

 

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.