Home > Backend Development > Python Tutorial > How to use the glob module for file pattern matching in Python 2.x

How to use the glob module for file pattern matching in Python 2.x

PHPz
Release: 2023-07-30 15:24:42
Original
1139 people have browsed it

Python is a powerful programming language that is widely used in data analysis, web development and other fields. In file operations, it is often necessary to match file names or file paths based on specific patterns. Python provides the glob module for file pattern matching. This article will introduce how to use the glob module for file pattern matching in Python 2.x.

The glob module is part of the Python standard library and can be used without additional installation. It provides file and folder search methods based on pattern strings and returns a list of matching files or folders. The glob module supports some common file pattern matching rules:

  • *: matches any character.
  • ?: Matches a single character.
  • [Character]: Matches any character in the character set.
  • [!Character]: Matches any character other than any character in the character set.

The following is a sample code for using the glob module for file pattern matching:

import glob

# 匹配当前目录下所有的.py文件
files = glob.glob('*.py')
print(files)

# 匹配当前目录下所有以test开头的文件夹
folders = glob.glob('test*')
print(folders)

# 匹配当前目录及其子目录下所有的.txt文件
files = glob.glob('**/*.txt', recursive=True)
print(files)
Copy after login

In the above code, the glob module is first imported. Then pass in the matching pattern string through the glob.glob() method to obtain a list of matching files or folders. Use *.py to match all .py files in the current directory, where * means matching any characters. Use test* to match all folders starting with test in the current directory, where * means matching any character. Use **/*.txt to match all .txt files in the current directory and its subdirectories, where ** means matching any level of subdirectories.

Run the above code to get a list of matching files or folders, which are stored in the files and folders variables respectively. Then print the result through the print function. In actual use, you can perform further operations on the matched files or folders as needed.

In addition to basic file pattern matching, the glob module also supports path expansion. For example, you can use ~ to represent the user's home directory, and ../ to represent the upper-level directory. The following is a sample code using path expansion:

import glob

# 匹配当前用户主目录下所有的.py文件
files = glob.glob('~/Desktop/*.py')
print(files)

# 匹配上一级目录下所有以test开头的文件夹
folders = glob.glob('../test*')
print(folders)
Copy after login

In the above code, use ~/Desktop/*.py to match all .py in the Desktop directory under the current user's home directory. document. Use ../test* to match all folders starting with test in the upper-level directory.

In summary, the glob module provides a convenient way to perform file pattern matching. With a simple pattern string, you can match file names or file paths and return a list of matching files or folders. Using the glob module in Python 2.x can easily implement file pattern matching and improve the efficiency of file operations.

The above is the detailed content of How to use the glob module for file pattern matching in Python 2.x. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template