List Files in a Directory in Python

List Every File in a Directory

It's simple as pie to get a directory's file list! To list all files in a directory, use an OS module's listdir() and isfile() functions. These are the procedures.

1) Add the OS module.

With the aid of this module, we can use Python's operating system-specific features. The OS module provides functions for dealing with the operating system.

2) Use the procedure os.listdir()

The list of files and directories in the directory indicated by the path is returned by the os.listdir('path') function.

3) Refine the outcome.

The listdir() function's returned files can be iterated using a for a loop. We will traverse each file given by the listdir() function using a loop.

4) Employ the isfile() method.

Use the os. path.isfile('path') function to determine whether the current approach is a file or directory for each loop iteration. Add it to a list if it's a file. If a path is a file, this function returns True. Otherwise, False is returned.

Example of a Directory File List

Let's practice listing the files in the "account" folder. Only files in the current directory will be recorded by listdir(); subdirectories will not be considered.

Example 1

List only the files in a directory

import os

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

# list to store files
res = []

# Iterate directory
for path in os.listdir(dir_path):
    # check if current path is a file
    if os.path.isfile(os.path.join(dir_path, path)):
        res.append(path)
print(res)

We have three file names here.

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

write your code here: Coding Playground

If you are familiar with generator expressions, you can use a generator function to create smaller, simpler code, as demonstrated below.

Generator Expression

import os

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

After that, just call it as needed.

for file in get_files(r'E:\\account\\'):
    print(file)

write your code here: Coding Playground

Example 2

List both files and directories

To access the contents of a directory, use the listdir('path') method directly.

import os

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

# list file and directories
res = os.listdir(dir_path)
print(res)

Output:

The output shows that "reports 2021" is a directory.

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

write your code here: Coding Playground

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

A generator that creates a tuple of values (current path, directories in current path, and files in current path) is returned by the os.walk() function.

Note: We may list all directories, subdirectories, and files in one directory by using the os. walk() function. Every time the generator is used, it will follow each directory recursively to get a list of files and directories until no more sub-directories are accessible from the initial directory because it is a recursive function.

For each directory that the os. Walk ('path') command traverses; two lists will be returned. Files are included in the first list, and directories are included in the second list.

Let's look at an example of how to list every file 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)

Output:

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

write your code here: Coding Playground

Using os.scandir() to retrieve files from a directory

For many typical use scenarios, the scandir() function returns directory entries along with file attribute information and offers improved speed.

It gives back an iterator of file name-containing os.DirEntry objects.

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)

Output:

profit.txt
sales.txt
sample.txt

write your code here: Coding Playground

Files in a Directory are listed by the Glob Module

To locate files and folders whose names match a given pattern, use the Python glob module from the Python Standard Library.

For instance, we will use the dire path/*.* pattern to retrieve all files in a directory. Any file with the extension *.* is meant here.

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)

Output:

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

write your code here: Coding Playground

Note: Set the recursive attribute to True if you want to list files from subdirectories.

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)

Output:

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

write your code here: Coding Playground

To list the files in a directory using Pathlib Module

We can utilize the pathlib module, which offers a wrapper for most OS functions, starting with Python 3.4.

1) Import the pathlib module: For many operating systems, the pathlib module provides classes and methods to manage filesystem paths and retrieve information about files.

2) Use the pathlib next.

Directory path construction using Path('path')

3) After that, use iterdir() to loop through each entry in the directory.

4) Finally, use the path.isfile() function to determine whether the current element is a file.

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)

write your code here: Coding Playground