Python glob standard library Basics Learning

Source: Internet
Author: User
Tags glob

#glob文件名模式匹配
#作用: Use Unix shell rules to find matching file names with a pattern
"""
Although the Glob API is small, this module is very powerful, as long as the program needs to find a file system in which the name matches a set of files in a certain pattern.
You can use this module to create a list of file names that require each file name to have a specific extension, a prefix, or a common string in the middle.
You can use Glob instead of writing custom code to scan directory content.
Glob mode is not the same as regular re-mode, actually. Glob mode follows standard UNIX path extension rules, using only
Several special characters are used to implement 2 different wildcard and character intervals, and the pattern rule is applied to the middle of the file name (at the path delimiter/at the cutoff).
The path in the pattern can be a relative/absolute path, and the shell variable name wavy line (~) will not be extended!!!!!
"""
#示例1
#测试在path/Files below
#名称如下path/a/a.txt b.txt c.txt d.txt/b/a.txt (just for a look)
Import Glob
#如果未有此文件, please create your own
Path= ' Path/a/*.txt '
Print Glob.glob (path)
#通配符 *: can match 0 or more of a filename segment, such as Dir/*.*, as above
#要列出子目录文件, the subdirectory must be included in the schema
print ' Path *: '
For name in Glob.glob (Pathname=path):
print ' \ t ', name

#匹配单字字符串
print '? '
Path= ' path/a/? txt
For name in Glob.glob (Pathname=path):
print ' \ t ', name
#匹配区间字符串 (if the interval is between [a~z], you can match a string in more than one
print ' [A-z] '
For path1 in Glob.glob (' Path/a/*[a-z0-9a-z].txt '):
Print path1
#glob官方标准地址: Https://docs.python.org/2.7/library/glob.html?highlight=glob#module-glob

#外接连 (Patten matching notation): http://pubs.opengroup.org/onlinepubs/000095399/utilities/xcu_chap02.html#tag_02_13
#其他: Explanation of File name pattern matching in the shell Command Language specification of open Grop
#fnmatch请百度或者谷歌

Python glob standard library Basics Learning

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.