How Python traverses all the files in the directory _python

Source: Internet
Author: User
Tags extend glob

Os.walk Generator
Os.walk (path), path is a folder path, of course you can use the. /So.
Returns a list of three-tuple elements, each representing the contents of a folder. The first is the contents of the current folder.
The returned ternary group representation (the working folder, the list of folders under that folder, the list of files under that folder).
So
To get all subfolders, that is (d represents the ternary group):

Os.path.join (d[0],d[1]);

To get all the sub files, you are:

 Os.path.join (d[0],d[2]);

The following example uses two sets of loops, iterates through the list of all filenames, and then loops through all the files:

result = [Os.path.join (DPS, F) for DPS, DN, FS in Os.walk ("_pages") for F in FS if Os.path.splitext (f) [1] = = '. html ']
fo R fname in:
 #do something

actually equals

Result=[]
for DPS, DN, FS in Os.walk ("_pages"): For
 F in fs:
 if (Os.path.splitext (f) [1] = = '. html '):
  R Esult.append (Os.path.join (DP, F)) for
fname in:
 #do something

Finally, determine if the HTML suffix gets the filename, and you can also use Glob:

result = [y for x in Os.walk (PATH) for y in Glob.glob (Os.path.join (x[0], ' *.txt ')]

You can also use the iterator method:

From Itertools import chain
import glob result = (chain.from_iterable Glob.iglob (Os.path.join (x[0
), ' *.txt ') For x in Os.walk ('. '))

Advanced
standard File Number traversal Builder Os.walk is both powerful and flexible, however, Os.walk also lacks the details of the processing power required by the application, such as selecting files based on a pattern, sorting all files (or directories), or simply traversing the current directory without entering its subdirectories, so that the interface needs to be encapsulated 。

 import os, Fnmatch def filter_files (dirname, patterns= ' * ', Single_level=false, YIELD_FO 
  Lders=false): Patterns = Patterns.split (';') 
    Allfiles = [] for RootDir, subdirname, files in Os.walk (dirname): Print Subdirname allfiles.extend (files) If Yield_folders:allfiles.extend (dubdirname) if Single_level:break allfiles.sort () for EACHPA 

Ttern in Patterns:for eachfile in Fnmatch.filter (Allfiles, Eachpattern): Print Os.path.normpath (eachfile) 

Description:
1.extend differs from append
The list is implemented as a class. The Create list is actually an instantiation of a class. Therefore, there are several ways that the list can be manipulated.   lists can contain elements of any data type, and elements in a single list need not be all of the same type. The append () method adds a new element to the tail of the list. Accepts only one argument, the Extend () method takes only one list as a parameter and adds each element of the parameter to the existing list. The
2 fnmatch module
Fnmatch module uses patterns to match file names. The schema syntax is the same as that used in the Unix shell. An asterisk (*) matches 0 or more characters, and a question mark (?) matches a single character. You can also use square brackets to specify a range of characters, for example, [0-9] represents a number, and all other characters match themselves.
1) Fnmatch.fnmatch (name, pattern) method: Test If name matches pattern, return True/false
2 fnmatch.filter (names, Pat) Implements filter or filter for list special characters, returns a list of characters that match the pattern, and of course names represents the list

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.