The demand is sauce:
Enter a directory that contains the necessary information to detect the directory but does not need to be accurate after processing, such as the directory: C:\Program Files\Common Files\designer, the directory to be detected is: C:\Program Files\Common Files\System, which gets the preceding information from the given directory, is specified by the following list of complements (System). Detects an XML file from the E:\res\tmp directory and returns the directory of the XML file
The code is as follows: 1 import os 2 import re
3PathList = []4Patternxml = R'. +\.xml$'5 6 defcollectxmlfile (path)7 ifRe.search (R'\\Program Files\\Common Files', Path)8Path = Path.split ('Common Files') [0]9L = [path,'\common Files\System']TenPath ="'. Join (L) OnePathlistfinal =PathList A forFileinchOs.listdir (path): -File =Os.path.join (path,file) - if(Os.path.isdir (file) = =False): the if(Re.match (patternxml,file)): - pathlistfinal.append (file) - Else:Pass - Else: + collectxmlfile (file) - returnpathlistfinal
+Collectxmlfile (R ' C:\Program Files\Common Files\designer ')
In this, why does the second time recursion to Collectxmlfile () not search match?
Because in line 9th, Common in front of a "\", so the second iteration of the path Common in front there will be two ' \ \ ', search R ' \\Program Files\\Common Files ' can only match a backslash in front of the common, but a multiple backslash in the path directory, for Windows directory recognition is not affected, os.listdir (path) will still correctly identify the directory, not because there is a backslash cannot find the directory, such as ' C:\Program Files\\Common Files\designer '.
But this ingenious way of easy to read, I looked for several days to figure out, it is more normal to put directory recognition outside the iterative function, processing and then pass the function of the iteration.
Python regular match recursion gets the specific type of file given in the directory tips