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, excluding the files in the 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.
Pathname Can 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 )
-
-
return an iterator which yields the same values as glob () 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 , And Card.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' ) Using '1.gif ', 'card.gif'] >>> Glob . Glob ( '?. GIF' ) Using '1.gif ']