Skip to content
Related Articles

Related Articles

Improve Article

Python – List files in directory with extension

  • Last Updated : 23 Aug, 2021

In this article, we will discuss different use cases where we want to list the files with their extensions present in a directory using python.

Modules Used

  • os: The OS module in Python provides functions for interacting with the operating system.
  • glob: In Python, the glob module is used to retrieve files/pathnames matching a specified pattern. The pattern rules of glob follow standard Unix path expansion rules. It is also predicted that according to benchmarks it is faster than other methods to match pathnames in directories.

Directory Structure in use:

Directory Structure Root View

Directory Files Visual Representation

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Method 1: Using `os` module

This module provides a portable way of using operating system-dependent functionality. The method os.listdir() lists all the files present in a directory. We can make use of os.walk() if we want to work with sub-directories as well.


 os.listdir(path = ‘.’)

Returns a list containing the names of the entries in the directory given by path. 


 os.walk(top, topdown=True, onerror=None, followlinks=False)

Generates the file names in a directory tree by walking the tree either top-down or bottom-up.

Example 1: List the files and directories present in root/home/project


import os
# To get directories as well as files present
# in a path
list_1 = os.listdir(path=r"root/home/project")
# To get only files present in a path
list_2 = os.listdir(path=r"root/home/project")
# Loop through each value in the list_2
for val in list_2:
    # Remove the value from list_2 if the "." is 
    # not present in value
    if "." not in val:

Output :

['documents', 'code', 'charter.xlsx', 'timeline.jpg']
['charter.xlsx', 'timeline.jpg']

Example 2: List all the subdirectories and sub-files present in root/home/project


import os
all_files = list()
all_dirs = list()
# Iterate for each dict object in os.walk()
for root, dirs, files in os.walk("root/home/project"):
    # Add the files list to the the all_files list
    # Add the dirs list to the all_dirs list


[‘charter.xlsx’, ‘timeline.jpg’, ‘report.txt’, ‘workbook.pdf’, ‘trigger.sql’, ‘’, ‘’, ‘info.log’, ‘’, ‘requirements.txt’, ‘’]

[‘documents’, ‘code’, ‘database_models’] 

Method 2: Using `glob` module

The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell. We will use glob.glob() function to achieve our task. The idea behind Unix shell-like means that we can provide Unix shell-like patterns for searching files.


 glob.glob(pathname, *, recursive=False)

Return a list of pathnames that match pathname, which must be a string containing a path specification.

The ‘*‘ means that it will match all the items returned by similar to os.listdir() method.

Example 1: Get all the directories and files in root/home/project/code


import glob
list_ = glob.glob(r"root/home/project/code/*")


[‘database_models’, ‘’, ‘requirements.txt’, ‘’]

Example 2: Get all the python (.py) files in root/home/project/code/database_models


import glob
list_ = glob.glob(r"root/home/project/code/database_models/*.py")


[‘’, ‘’]

My Personal Notes arrow_drop_up
Recommended Articles
Page :