PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » File Handling » Python List Files in a Directory

Python List Files in a Directory

Updated on: February 24, 2024 | 1 Comment

To list all files in a directory using Python, you can use the built-in os module.

Also, there are multiple ways to list files in a directory. In this article, We will use the following four methods.

  • os.listdir('dir_path'): Return the list of files and directories in a specified directory path.
  • os.walk('dir_path'): Recursively get the list of all files in a directory and subdirectories.
  • os.scandir('path'): Returns directory entries along with file attribute information.
  • glob.glob('pattern'): glob module to list files and folders whose names follow a specific pattern.

Table of contents

  • How to List All Files in a Directory using Python
    • Example: List Files in a Directory
  • os.walk() to list all files in a directory and subdirectories
  • Get a list of files in current directory in Python
  • os.scandir() To get the list of files in a directory
  • List Files of a Directory that follow a specific pattern
  • Pathlib Module to list files of a directory

How to List All Files in a Directory using Python

Use the listdir() and isfile() functions of an os module to list all files in a directory. Here are the steps.

  1. Import os module

    First, import the os module. This module helps us to work with operating system-dependent functionality in Python. The os module provides functions for interacting with the operating system.

  2. Decide the path to the directory

    Next, decide the path to the directory you want to list the files of. Make sure you use the correct format for your operating system. For example, on Windows, paths are typically written with backslashes (e.g., 'C:\\path\\to\\your\\directory').

  3. Use os.listdir(‘directory_path’) function

    Now use the os.listdir('directory_path') function to get a list of the names of the entries ( the files and directories ) in the directory given by the directory_path.

  4. Iterate the result

    Next, Use for loop to Iterate the list returned by the os.listdir(directory_path) function. Using for loop we will iterate each entry returned by the listdir() function.

  5. Use isfile(path) function

    Now, In each loop iteration, use the os.path.isfile('path') function to check whether the current entry is a file or a directory. If it is a file, then add a filename to a list. This function returns True if a given path is a file. Otherwise, it returns False.

Example: List Files in a Directory

Let’s see how to list all files in an ‘account’ directory. This example will list files present in the current directory and ignore the subdirectories.

Example 1: List only files in a directory

import os

# directory/folder path
dir_path = r'E:\account'

# list to store files
res = []

# Iterate directory
for file_path in os.listdir(dir_path):
    # check if current file_path is a file
    if os.path.isfile(os.path.join(dir_path, file_path)):
        # add filename to list
        res.append(file_path)
print(res)Code language: Python (python)

Output:

Here we got three file names.

['profit.txt', 'sales.txt', 'sample.txt']

Note:

  • The os.listdir(dir_path) will list files only in the current directory and ignore the subdirectories.
  • The os.path.join(directory, file_path) is used to get the full path of the entry.

Read more: Python list files in a directory with extension txt.

Also, you should handle the case where the directory does not exist or an error occurs while accessing it. For this, you can use a try-except block. Here’s how you can modify the code:

import os


def list_files(dir_path):
    # list to store files
    res = []
    try:
        for file_path in os.listdir(dir_path):
            if os.path.isfile(os.path.join(dir_path, file_path)):
                res.append(file_path)
    except FileNotFoundError:
        print(f"The directory {dir_path} does not exist")
    except PermissionError:
        print(f"Permission denied to access the directory {dir_path}")
    except OSError as e:
        print(f"An OS error occurred: {e}")
    return res


# directory/folder path
dir_path = r'E:\account'

files = list_files(dir_path)
print('All Files:', files)
Code language: Python (python)

Generator Expression:

If you know generator expression, you can make code smaller and simplers using a generator function as shown below.

import os

def get_files(path):
    for file in os.listdir(path):
        if os.path.isfile(os.path.join(path, file)):
            yield fileCode language: Python (python)

Then simply call it whenever required.

for file in get_files(r'E:\\account\\'):
    print(file)Code language: Python (python)

Example 2: List both files and directories.

Directly call the listdir('path') function to get the content of a directory.

import os

# folder path
dir_path = r'E:\\account\\'

# list file and directories
res = os.listdir(dir_path)
print(res)Code language: Python (python)

Output:

As you can see in the output, ‘reports_2021’ is a directory.

['profit.txt', 'reports_2021', 'sales.txt', 'sample.txt']

os.walk() to list all files in a directory and subdirectories

You can use os.walk() function in Python to get a list of files in a directory and all of its subdirectories/subfolders.

The os.walk() function returns a generator that creates a tuple of values (current_path, directories in current_path, files in current_path).

It is a recursive function, i.e., every time the generator is called, it will follow each directory recursively to get a list of files and directories until no further sub-directories are available from the initial directory.

This function will yield the paths to all files in the given directory and recursively in all subdirectories. For example, calling the os.walk('path') will yield two lists for each directory it visits. The first list contains files, and the second list includes directories.

Let’s see the example of how to list all files in a directory and its subdirectories.

Example:

from os import walk

# folder path
dir_path = r'E:\\account\\'

# list to store files name
res = []
for (dir_path, dir_names, file_names) in walk(dir_path):
    res.extend(file_names)
print(res)Code language: Python (python)

Output:

['profit.txt', 'sales.txt', 'sample.txt', 'december_2021.txt']

Note:

  • The above example will include every file in the directory and its subdirectories, so if there are a lot of files, this could potentially take a long time.
  • Also, add a break statement inside a loop to stop looking for files recursively inside specific subdirectories if those files are not required.

Example:

from os import walk

# folder path
dir_path = r'E:\\account\\'
res = []
for (dir_path, dir_names, file_names) in walk(dir_path):
    res.extend(file_names)
    # don't look inside any subdirectory
    break
print(res)
Code language: Python (python)

Get a list of files in current directory in Python

You can use the os module and the listdir() function to get a list of files in the current directory in Python. Here’s an example.

import os

# list to store filename present in current directory
files = []
for file_path in os.listdir('.'):
    if os.path.isfile(os.path.join('.', file_path)):
        files.append(file_path)
for file in files:
    print(file)Code language: Python (python)

In this code:

  • '.' refers to the current directory.
  • os.listdir('.') returns a list of all files and directories in the current directory.
  • os.path.isfile(f) checks if each entry is a file (as opposed to a directory).

os.scandir() To get the list of files in a directory

The scandir() function returns directory entries along with file attribute information, giving better performance for many common use cases.

It returns an iterator of os.DirEntry objects, which contain file names.

Example:

import os

# get all files inside a specific folder
dir_path = r'E:\\account\\'
for path in os.scandir(dir_path):
    if path.is_file():
        print(path.name)Code language: Python (python)

Output:

profit.txt
sales.txt
sample.txt

List Files of a Directory that follow a specific pattern

The Python glob module, part of the Python Standard Library, is used to find the files and folders whose names follow a specific pattern.

For example, to get all files of a directory, we will use the dire_path/*.* pattern. Here, *.* means file with any extension.

Read more: Python list files in a directory with extension txt.

Let’s see how to list files from a directory using a glob module.

Example:

import glob

# search all files inside a specific folder
# *.* means file name with any extension
dir_path = r'E:\account\*.*'
res = glob.glob(dir_path)
print(res)Code language: Python (python)

Output:

['E:\\account\\profit.txt', 'E:\\account\\sales.txt', 'E:\\account\\sample.txt']

Note: If you want to list files from subdirectories, then set the recursive attribute to True.

Example:

import glob

# search all files inside a specific folder
# *.* means file name with any extension
dir_path = r'E:\demos\files_demos\account\**\*.*'
for file in glob.glob(dir_path, recursive=True):
    print(file)Code language: Python (python)

Output:

E:\account\profit.txt
E:\account\sales.txt
E:\account\sample.txt
E:\account\reports_2021\december_2021.txt

Pathlib Module to list files of a directory

From Python 3.4 onwards, we can use the pathlib module, which provides a wrapper for most OS functions.

  • Import pathlib module: Pathlib module offers classes and methods to handle filesystem paths and get data related to files for different operating systems.
  • Next, Use the pathlib.Path('path') to construct a directory path
  • Next, Use the iterdir() to iterate all entries of a directory
  • In the end, check if a current entry is a file using the path.isfile() function

Example:

import pathlib

# folder path
dir_path = r'E:\\account\\'

# to store file names
res = []

# construct path object
d = pathlib.Path(dir_path)

# iterate directory
for entry in d.iterdir():
    # check if it a file
    if entry.is_file():
        res.append(entry)
print(res)Code language: Python (python)

Filed Under: Python, Python File Handling

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python File Handling

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Loading comments... Please wait.

In: Python Python File Handling
TweetF  sharein  shareP  Pin

  Python File Handling

  • File Handling In Python
  • Create File in Python
  • Open a File in Python
  • Read File in Python
  • Write to File in Python
  • Python File Seek
  • Rename Files in Python
  • Delete Files in Python
  • Copy Files in Python
  • Move Files in Python
  • List Files in a Directory
  • File Handling Quiz

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

Python Basics Python Exercises Python Quizzes Python Interview Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com