Home  >  Article  >  Backend Development  >  How to Recursively Search Sub-Folders and Retrieve Files in Python?

How to Recursively Search Sub-Folders and Retrieve Files in Python?

Susan Sarandon
Susan SarandonOriginal
2024-10-30 04:12:28162browse

How to Recursively Search Sub-Folders and Retrieve Files in Python?

Recursive Sub-Folder Search and File Retrieval

In the realm of programming, traversing directories and retrieving files can be a common task. One efficient approach is to employ a recursive algorithm to search through sub-folders and accumulate a list of files meeting specific criteria.

Problem Encountered

A user encountered a roadblock while attempting to build a list of specific files in a recursive sub-folder search. The issue stemmed from the subfolder variable pulling in a list of subfolders instead of the current folder containing the file.

Purpose of subFolder

The subFolder variable is intended to hold the name of the sub-folder under the main folder where the desired file is located.

Resolution

The key to resolving this issue lies in leveraging the "root" or "dirpath" variable instead of "subFolder." This variable holds the absolute path to the current directory being traversed during the recursion. By incorporating this path into the construction of the fileNamePath, the desired accuracy can be achieved.

<code class="python">import os
result = [os.path.join(dp, f) for dp, dn, filenames in os.walk(PATH) for f in filenames if os.path.splitext(f)[1] == '.txt']</code>

Glob Module Enhancement

An alternative and efficient approach involves utilizing Python's built-in glob module. Glob excels at selecting files based on their extensions.

<code class="python">import os
from glob import glob
result = [y for x in os.walk(PATH) for y in glob(os.path.join(x[0], '*.txt'))]</code>

Generator Function

For a concise and efficient implementation, one can also employ a generator function.

<code class="python">from itertools import chain
result = (chain.from_iterable(glob(os.path.join(x[0], '*.txt')) for x in os.walk('.')))</code>

Python 3.4 Solution

If you're working with Python 3.4 or later, you can leverage the pathlib module's rglob() function.

<code class="python">from pathlib import Path
result = list(Path(".").rglob("*.[tT][xX][tT]"))</code>

These approaches provide effective solutions for recursively searching sub-folders and compiling a list of files meeting the desired criteria, making your file retrieval tasks a breeze.

The above is the detailed content of How to Recursively Search Sub-Folders and Retrieve Files in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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