Python regular match recursion gets the specific type of file given in the directory tips

Source: Internet
Author: User

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

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.