All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python List All Files In Directory And Subdirectories With Extension

Last Updated : Mar 11, 2024

Python List All Files In Directory And Subdirectories With Extension

In this article we will show you the solution of python list all files in directory and subdirectories with extension, before doing any activity on a file, we occasionally need to list all of the files with that particular extension.

Consider the scenario where you merely needed to copy text files between two locations. In this instance, we must be certain that we are just searching for files with a.txt extension.

Step By Step Guide On Python List All Files In Directory And Subdirectories With Extension :-

A file's suffix, also known as its filename extension, is called a filename.

The period is followed by it. A file type is identified by its extension, such as text, CSV, PDF, and image file. As an illustration, it is txt for a text file. JPG, PNG, or BMP are the available image file types.

Method - Glob module to list files from subdirectories with txt extension

import glob
path = r'E:/account/**/*.txt'
files = glob.glob(path, recursive=True)
print(files)
  1. The glob module is imported on the first code line. The Python Standard Library's glob module is used to locate files and directories whose names match a certain pattern.
  2. The path expansion rules for the Unix Shell are identical to the searching rules.
  3. After that, we employ path, which provides glob module support for the ** directive. Use glob.glob('**/*.txt') and add the recursive flag to True to make it recursive.
  4. The glob() method parses the specified path and searches recursively in the folders.
  5. Finally, using the glob() method, which produces a list of files matching the path and pattern supplied in the pathname argument, is what we do next. All text files will be returned in this scenario.
  6. Next, we list text files in subdirectories, we set a glob() method's recursive property to True.
  7. Next, using the print command, we print those text files found in the directory and any subdirectories.

Method - os.walk() to list files in directory and subdirectories with extension txt

import os
# list to store txt files
res = []
for root, dirs, files in os.walk(r"E:\demos\files_demos\account"):
    for file in files:
        if file.endswith(".txt"):
            res.append(os.path.join(root, file))
print(res)
  1. The first line of our code import the OS module.
  2. Then, to store text files, we utilise the res list.
  3. as a result The os.walk('path') function should be used. For every directory it hits, it will provide two lists. Files are included in the first list, and directories are included in the second list.
  4. Next, use a for loop to iterate through the list of files.
  5. Then, in each cycle, utilise the if condition to see if the file ends in.txt. In that case, include it in the final list.
  6. Finally, we print every file using the print function.

Conclusion :-

As a result, we have successfully learnt how to list all files in a directory and its subdirectories, along with their extensions, in Python.

We also discovered that the OS walk function is recursive, i.e., each time this same generator is called, it generates a tuple of attributes consisting of the current path, directories in the current path, and files in the current path.

The generator then proceeds to recursively follow each directory to obtain a list of directories and files until no more sub-directories are accessible from the first directory.

I hope this article on python list all files in directory and subdirectories with extension helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪