深度阅读

How to use Glob() function to find files recursively in Python?

作者
作者
2023年08月22日
更新时间
7.88 分钟
阅读时间
0
阅读量

To use the glob() function to find files recursively in Python, you can use the ** wildcard character in the search pattern. Here’s an example:

import glob

# finds all .txt files recursively in a directory
files = glob.glob('/path/to/directory/**/*.txt', recursive=True)

# prints the list of matching file names
print(files)

In this example, we import the glob module and use its glob() function to search for all files with the .txt extension in the directory /path/to/directory, including all subdirectories. We pass the recursive=True parameter to search recursively. The function returns a list of matching file names, which we print to the console.

Make sure you have Python 3.5 or higher to use the ** character to search recursively with glob().

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。