Python glob fnmatch for file lookup operations

Source: Internet
Author: User
Tags glob

Reference:

Http://python.jobbole.com/81552/:Python Module Learning: Glob file path Lookup

A handy file name Operation module in Http://blog.csdn.net/suiyunonghen/article/details/4517103:python Glob

https://docs.python.org/2/library/glob.html:10.7. Glob-unix Style pathname Pattern expansion

https://docs.python.org/2/library/fnmatch.html#module-fnmatch:10.8. Fnmatch-unix filename pattern Matching


################################################################ #3


Distribution of test set files:



3############################################################# #3


I glob source location:/usr/lib/python2.7/glob.py



Function Name: Glob

Parameter name: pathname-path name (can use relative or absolute path)

Function: Returns a list that stores all file paths that match your parameter name (when you use a relative path, the file path is a relative path; When you use an absolute path, the file path is the absolute path)

Note: The scope of the lookup is only in the directory where the path name resides


The pathname must use a wildcard character, but only three (*/?/[]). * represents all files,? represents a character, [] denotes the number of characters that you want to match (for example, [0-9] indicates a match number 0-9)


Find all files and directories in the current directory:



Note: It is known that only the path is provided and no results are obtained


To find the TXT file in the current directory:



There are three levels of known test sets

Find all txt files on the second tier:



The function Iglob (pathname) function is similar to Glob, except that Iglob returns an iterator. See the source, two functions are actually exactly the same

def glob (pathname): "" "    Return a list of paths matching a pathname pattern.    The pattern may contain simple shell-style wildcards a la    fnmatch. However, unlike Fnmatch, filenames starting with a    dot is special cases that is not matched by ' * ' and '? '    Patterns.    "" " Return list (Iglob (pathname))

########################################################################## #33


I fnmatch source location:/usr/lib/python2.7/fnmatch.py

A total of four functions:



Function Filter:


Returns a subset of the compliant-mode pat in the list names



function Fnmatch


Test whether name conforms to Pat's pattern

The PAT format conforms to the UNIX text type:

*-Indicates all

?-Represents a single character

[seq]-denotes a character that conforms to a sequence within []

[!seq]-indicates a character that does not conform to the sequence within []


Check the source code, the function is the same as Fnmatcncase:

def fnmatch (name, Pat): "" "    Test whether FILENAME matches PATTERN.    Patterns is Unix shell style:    *       matches everything    ?       Matches any single character    [seq]   matches any character in Seq    [!seq]  matches no char    not in seq an Initial period in FILENAME was not special.    Both FILENAME and PATTERN are first case-normalized    if the operating system requires it.    If you don ' t want this, use Fnmatchcase (FILENAME, PATTERN).    "" " Import OS    name = os.path.normcase (name)    Pat = os.path.normcase (PAT)    return Fnmatchcase (name, Pat)

function Fnmatchcase


No normalization parameters, just determine if the file name matches the pattern character, including case



Note: Parameters are a single string


Python glob fnmatch for file lookup operations

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.