Python module learning: glob find the file path

Original link: http://www.cnblogs.com/guolei2570/p/8723221.html

glob module is one of the simplest module, the content is very small.

Use it to find the file path name in line with specific rules. With files in windows search using the same.

Find files matching only uses three characters: "?", "*", "[]."

'*' Matches zero or more characters; match a single character; "[]" matches the specified range of characters, such as "?": [0-9] matching digits.

glob.glob

Return path for a list of all matching files. It has only one argument pathname, file paths defined matching rules, there can be an absolute path, or a relative path. The following are examples of the use glob.glob:

1  Import glob
 2   
3  # Gets the specified directory of all the pictures 
4  Print ( glob.glob (r " E:. / Picture / * / * JPG " ))
 5   
6  # get the parent directory of all .py files 
7  Print ( glob .glob (R & lt ' ../*.py ' )) # relative path

glob.iglob

Gets a programmable calendar objects with which you can get the file path name that matches one by one. The difference between glob.glob () is: glob.glob simultaneously acquire all of the matching path, but only once glob.iglob get a matching path. This is somewhat similar operations in .NET DataSet and DataReader database used. The following is a simple example:

. 1  Import glob
 2   
. 3  # parent directory .py files 
. 4 F = glob.iglob (R & lt ' ../*.py ' )
 . 5   
. 6  Print ( F) # <Object Generator iglob AT 0x00B9FF80> 
. 7   
. 8  for Py in F :
 9      Print ( Py)

 

Reproduced in: https: //www.cnblogs.com/guolei2570/p/8723221.html

Guess you like

Origin blog.csdn.net/weixin_30707875/article/details/94793072