All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Python List All Files In Directory And Subdirectories

Last Updated : Mar 11, 2024

Python List All Files In Directory And Subdirectories

In this article we will show you the solution of python list all files in directory and subdirectories, the os listdir function in the Python os module can be used to obtain a list of the files or folders included in a directory.

The return response lists each file and subdirectory in the supplied path.

To build an exhaustive list of files in a given directory tree, we must run this recursively for subdirectories.

The os walk method, which iterates through a directory tree, is available in the OS module of Python.

It iterates through the directory tree just at specified path, returning a tuple for every directory or subdirectory.

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

Method: Using glob module

import glob
rootdir = ‘C://Users//Dell//OneDrive//Desktop’
for path in glob.glob(f'{rootdir}/*/'):
  1. The glob module is imported on the initial line of code. The function known as the glob module, that stands for global, is used to look for files which match a given file pattern and name. Both CSV files and text in files can be searched with it.
  2. Next, we utilise rootdir to find the location of the files. A computer's files can be stored, arranged, and divided using directories. The root directory is the one that doesn't have a parent.
  3. Then, we employ a for loop with glob.glob function, which produces an iterator over a collection of pathnames that adhere to the given pattern.
  4. The print statement is used in the final line of code to print a listing of all files in the directory and its subdirectories.

Method: Using os.scandir() function

import os
rootdir = ‘C://Users//Dell//OneDrive//Desktop’
for it in os.scandir(rootdir):
    if it.is_dir():
  1. We start code by writing the import os. The OS module is a piece of the standard library for the Python programming language.
  2. When imported, it enables user interaction with the native operating system that Python is presently running on. Simply said, it gives the user a straightforward interface for interacting with a number of OS services that are useful for day-to-day programming.
  3. After that, we use rootdir to locate the files. Directories can be used to organise, categorise, and store files on a computer. The root directory is the one that doesn't have a parent.
  4. After that, we employ the os.scandir() function, that provides noticeably higher speed than os.listdir (). It provides file attribute data along with directory entries.
  5. Next, use the is dir() method to filter the items returned to only include directories or symbolic links leading to directories. If the current entry is a directory or simply a symbolic link pointing to a directory, the method returns True.
  6. With that print statement in the final sentence, we print the list.

Conclusion :-

As a result, we were able to understand the Python notion of listing all files in a directory and its subdirectories.

We also discovered how to get a list of the folders and files contained within a directory using the os listdir method found inside the Python os module.

In addition, Python's OS module provides a os walk function, that iterates through some kind of directory tree.

It returns a tuple for each directory or subdirectory after iterating through to the directory tree only once at the supplied path.

I hope this article on python list all files in directory and subdirectories helps you and the steps amd 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 🡪