Analysis of glob in python standard library

The glob file name pattern matches, without traversing the entire directory to determine whether each file matches.

1、 Wildcard

An asterisk (*) matches zero or more characters

import glob
for name in glob.glob('dir/*'):print(name)

dir/file.txt
dir/file1.txt
dir/file2.txt
dir/filea.txt
dir/fileb.txt
dir/subdir

To list files in a subdirectory, you must include the subdirectory name in the pattern:

import glob

# Query files with subdirectories
print('Named explicitly:')for name in glob.glob('dir/subdir/*'):print('\t', name)
# Use wildcards*Instead of subdirectory name
print('Named with wildcard:')for name in glob.glob('dir/*/*'):print('\t', name)

Named explicitly:
 dir/subdir/subfile.txt
Named with wildcard:
 dir/subdir/subfile.txt

2、 Single character wildcard

Use a question mark (?) to match any single character.

import glob

for name in glob.glob('dir/file?.txt'):print(name)

dir/file1.txt
dir/file2.txt
dir/filea.txt
dir/fileb.txt

3、 Character range

When you need to match a specific character, you can use a range

import glob
for name in glob.glob('dir/*[0-9].*'):print(name)

dir/file1.txt
dir/file2.txt

Knowledge point supplement: Python programming: glob module for file name pattern matching

File preparation

mkdirtmp cd tmp touchfile1.txt touch file2.txt touchfile3.log ls file1.txt file2.txt file3.log

test

import glob

# Use zero or more character wildcards* 
glob.glob("tmp/*.txt")
Out[1]:['file1.txt','file2.txt']

# Use single-character wildcards?
glob.glob("tmp/file?.txt")
Out[2]:['file1.txt','file2.txt']

# Use range matching
glob.glob("tmp/file[0-9].txt")
Out[3]:['file1.txt','file2.txt']

to sum up

So far, this article on the analysis of glob in the python standard library is introduced. For more relevant python standard library glob content, please search for ZaLou.Cn's previous articles or continue to browse related articles below. Hope you will support ZaLou more in the future .Cn!

Recommended Posts

Analysis of glob in python standard library
Basic analysis of Python turtle library implementation
Detailed explanation of python standard library OS module
Analysis of common methods of Python operation Jira library
Python analysis of wav files
Analysis of JS of Python crawler
Analysis of Python Sandbox Escape
Analysis of Python object-oriented programming
The usage of wheel in python
Summary of logarithm method in Python
Analysis of usage examples of Python yield
Use of numpy in Python development
Analysis of Python conditional control statements
Detailed usage of dictionary in Python
Usage of os package in python
A summary of 200 Python standard libraries!
​Full analysis of Python module knowledge
The usage of tuples in python
Description of in parameterization in python mysql
Understanding the meaning of rb in python
Implementation of JWT user authentication in python
The usage of Ajax in Python3 crawler
Python novices learn standard library module naming
Detailed analysis of Python garbage collection mechanism
Method of installing django module in python
The essence of Python language: Itertools library
Analysis of common methods of Python multi-process programming
Knowledge points of shell execution in python
The specific method of python import library
Detailed explanation of the use of pip in Python | summary of third-party library installation
Method analysis of Python calling C language program
What is the function of adb in python
Functions in python
7 features of Python3.9
Python implements the shuffling of the cards in Doudizhu
Python data analysis
The consequences of uninstalling python in ubuntu, very
Python implementation of AI automatic matting example analysis
Example of feature extraction operation implemented in Python
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
03. Operators in Python entry
Join function in Python
12. Network Programming in Python3
print statement in python
Python linear interpolation analysis
Basics of Python syntax
Concurrent requests in Python
Basic syntax of Python
Basic knowledge of Python (1)
Install python in Ubuntu
Context management in Python
Prettytable module of python
Arithmetic operators in python
Write gui in python
MongoDB usage in Python
09. Common modules of Python3
Str string in Python
Computational Geometry in Python
How to find the area of a circle in python