Home > Backend Development > Python Tutorial > How to Efficiently Find All \'.txt\' Files in Subfolders Using Python?

How to Efficiently Find All \'.txt\' Files in Subfolders Using Python?

Patricia Arquette
Release: 2024-10-29 19:28:02
Original
941 people have browsed it

How to Efficiently Find All

How to Efficiently Recursively Search Subfolders for Specific File Types

When scripting, it often becomes necessary to deeply search through subfolders within a main folder. In Python, the os.walk function can traverse directories recursively. However, extracting only files of a specific type can be tricky.

In the example provided, the goal is to create a list of all ".txt" files within a main folder and its subfolders. The code uses os.walk to iterate through all files and subfolders, but subFolder holds a list of subfolders instead of the correct subfolder for each file.

To resolve this, the "root" variable, which represents the current directory path, should be utilized instead of subFolder. Each file has its corresponding root, which indicates its location within the file system.

Here's an optimized solution:

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']
Copy after login

This code iterates through all root directories, subdirectories, and files in the PATH folder. Files with the ".txt" extension are added to the result list.

Alternatively, the glob module can simplify extension-based selection:

from glob import glob
result = [y for x in os.walk(PATH) for y in glob(os.path.join(x[0], '*.txt'))]
Copy after login

For Python 3.4 , a more concise solution using Pathlib is available:

from pathlib import Path
result = list(Path(".").rglob("*.[tT][xX][tT]"))
Copy after login

These solutions efficiently search subfolders, extract files based on extension, and return them as a list.

The above is the detailed content of How to Efficiently Find All \'.txt\' Files in Subfolders Using Python?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template