Python List All Files In Directory And Subdirectories With Extension
Last Updated : Jul 1, 2023
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)
- 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.
- The path expansion rules for the Unix Shell are identical to the searching rules.
- 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.
- The glob() method parses the specified path and searches recursively in the folders.
- 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.
- Next, we list text files in subdirectories, we set a glob() method's recursive property to True.
- 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)
- The first line of our code import the OS module.
- Then, to store text files, we utilise the res list.
- 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.
- Next, use a for loop to iterate through the list of files.
- 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.
- Finally, we print every file using the print function.
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 ﬁrst 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.