Python basics: Match rules-compliant files in the specified directory, print file full path

Source: Internet
Author: User
Tags log4j

# -*- coding:utf-8 -*-#遍历目录树import os,fnmatchdef all_files(root, patterns=‘*‘, single_level=False, yield_folder=False): # 将模式从字符串中取出放入列表中 patterns = patterns.split(‘;‘) for path, subdirs, files in os.walk(root): if yield_folder: files.extend(subdirs) files.sort() for fname in files: for pt in patterns: if fnmatch.fnmatch(fname, pt): yield os.path.join(path, fname) break if single_level: break# fnmatch 来检查文件名匹配模式# os.path fnmatch os.walk 生成器thefile=list(all_files(‘E:/projects/test-log4j‘, ‘*.class;*.java;*.properties;*.xml‘))for item in thefile: print item

The test-log4j application directory under the idea of Java is selected to print all the Java, class, properties, and XML file full paths below it.

E:\projects\py>python traverse.py

E:/projects/test-log4j\pom.xml
E:/projects/test-log4j.idea\compiler.xml
E:/projects/test-log4j.idea\misc.xml
E:/projects/test-log4j.idea\modules.xml
E:/projects/test-log4j.idea\workspace.xml
E:/projects/test-log4j.idea\copyright\profiles_settings.xml
E:/projects/test-log4j.idea\libraries\maven__log4j_log4j_1_2_17.xml
E:/projects/test-log4j.idea\markdown-navigator\profiles_settings.xml
E:/projects/test-log4j\src\main\java\main.java
E:/projects/test-log4j\src\main\resources\log4j.properties
E:/projects/test-log4j\src\main\resources\log4j.xml
E:/projects/test-log4j\target\classes\main.class
E:/projects/test-log4j\target\classes\log4j.properties
E:/projects/test-log4j\target\classes\log4j.xml

Python basics: Match rules-compliant files in the specified directory, print file full path

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.