A simple grasp of the use of Glob modules in Python to find file paths _python

Source: Internet
Author: User
Tags glob in python

Glob uses the Unix shell rule to find a file name that matches a pattern. This module is available whenever the program needs to find a set of files in the file system that match the name to a pattern.
Glob pattern rules are not the same as regular expressions used by the RE module. The glob pattern follows standard UNIX path extension rules. Only a few special characters are used to implement two different wildcard and character intervals. Pattern rules are applied to the segments in the file name. The path in the pattern can be a relative or absolute path.
Both the shell variable name and the wavy line do not expand.

Basic usage

1.glob.glob (pathname), returns a list of all matching file paths. It has only one parameter pathname, defines the file path matching rule, here can be absolute path, also can be a relative path.
2.glob.iglob (pathname), gets a calendar object that can be used to get the matching file path names individually. The difference from Glob.glob () is that Glob.glob gets all the matching paths at the same time, and Glob.iglob gets only one matching path at a time.

3.eg:

Import glob 
 
print Glob.glob (R ' E:\*\*.doc ') 
print Glob.glob (R '. \*.py ') 
 
f = Glob.iglob (R. \*.py ') for 
 
Py in F: 
  print py 

Run Result:

[' E:\\test_file\\adplus.doc '] 
['. \\perfrom_test.py ', '. \\pyTest.py ', '. \\simulation_login.py ', '. \\widget.py ', '. \\__init__.py '] 
. \perfrom_test.py. 
\pytest.py. 
\simulation_login.py 
\widget.py. 
\__init__.py 

Here we divide the knowledge point in detail:


wildcard characters
the asterisk matches 0 or more characters in a file name segment.

Import Glob for
name in Glob.glob (' tmp/* '):
  print Name

This pattern matches all pathname, but does not recursively search for subdirectories.

>>> ================================ Restart ================================
>>> 
tmp\ checklog_status.sh
tmp\check_adwords_v1.2.sh
tmp\check_traffic.sh
tmp\cut_nginxlog_v1.2.sh
tmp\ip_conn.sh
tmp\ip_keepalive.sh
tmp\nagios use manual. doc
tmp\nmap_ping
tmp\nrpe_ install-1.3.sh
tmp\one
tmp\syn.sh
tmp\zabbix_agentd_2.0.10_win_v1.2.bat
tmp\zabbix_agentd_ 2.0.8_v1.3.sh
tmp\ work content. doc

To list files in a subdirectory, you must include the directory in the schema.

Import glob
print ' name explicitly: '
for name in Glob.glob (' tmp/one/* '):
  print ' \ t ', name
print ' name With wildcard: '
to name in Glob.glob (' tmp/*/* '):
  print ' \ t ', name 

The first scenario shows the subdirectory name listed, and the second case relies on a wildcard to find the directory.

>>> ================================ Restart ================================
>>> 
Name Explicitly:
  tmp/one\another.txt
  tmp/one\file.txt
Name with wildcard:
  tmp\one\another.txt
  Tmp\one\file.txt


Single byte wildcard character
the question mark matches a single character in that position in the file name.

Import Glob for
name in Glob.glob (' tmp/chec?_traffic.sh '):
  print Name
>>> ================================ Restart ================================
>>> 
tmp \check_traffic.sh



Character Range
using a character interval ([A-z]), you can match one character in more than one character.

Import Glob for
name in Glob.glob (' tmp/one/[a-z]* '):
  print Name

The interval can match all lowercase letters.

>>> ================================ Restart ================================
>>> 
tmp/ One\another.txt
Tmp/one\file.txt

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.