How to list the files in a directory in Python

Posted in Python by Dirk - last update: Feb 13, 2024

There are several ways to list files in a directory in Python. The most obvious method is to use the os.listdir() function from the built-in os module. This function returns a list of file and directory names in directory with the specified path.

The different options are discussed in more detail below

Using the ‘os’ module

List all files and directories:

import os

path = "/path/to/directory"

# Get the list of all files and directories in the specified path
contents = os.listdir(path)
print(contents)

In this example:

  • os.listdir(path): This function from the os module returns a list of the names of the entries in the given directory path.
  • print(contents): This prints the list of files and directories to the console.

If you have a the following directory /documents/

  • documents
    • subfolder1
      • file_in_subfolder1.txt
    • subfolder2
    • file1.txt
    • file2.txt
    • file3.doc

The output will be:

['subfolder1','subfolder2','file1.txt','file2.txt','file3.doc']

List only files

import os

path = "/path/to/directory"

# Filter out only files from the list
files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
print(files)

How does this work:

  • os.path.isfile(os.path.join(path, f)): This checks if the current item in the list is a file (not a directory) by using os.path.isfile().
  • The list comprehension [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))] creates a new list containing only files from the original list of contents.

The output will be:

['file1.txt','file2.txt','file3.doc']

List only directories

import os

path = "/path/to/directory"

# Filter out only directories from the list
directories = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]
print(directories)
  • os.path.isdir(os.path.join(path, d)): This checks if the current item in the list is a directory by using os.path.isdir().
  • The list comprehension [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))] creates a new list containing only directories from the original list of contents.

Output:

The output will be:

['subfolder1','subfolder2']

List files / directories with full path

Using os.path.joinyou can get the full path rather than just the file names:


import os

path = "/path/to/directory"

# List all files and directories with their full paths
contents_with_paths = [os.path.join(path, f) for f in os.listdir(path)]
print(contents_with_paths)

How does it work:

  • os.path.join(path, f): This function joins the directory path with each file or directory name to create the full path. The list comprehension [os.path.join(path, f) for f in os.listdir(path)] creates a new list containing the full paths of all files and directories in the specified path.

Output:

['/path/to/directory/subfolder1', '/path/to/directory/subfolder2', 
 '/path/to/directory/file1.txt', '/path/to/directory/file2.txt',
 '/path/to/directory/file3.doc']

Also list files in all subdirectories

With os.walk you can list the files in the subdirectories

import os

path = "/path/to/directory"

# List all files in the specified directory and its subdirectories
all_files = [os.path.join(root, file) for root, dirs, files in os.walk(path) for file in files]
print(all_files)

How does it work:

  • os.walk(path): This function generates the file names in a directory tree, including the file names in all subdirectories.
  • The outer loop for root, dirs, files in os.walk(path) iterates through all directories and files in the specified path and its subdirectories.
  • The inner loop for file in files iterates through all the files in the current directory.
  • The list comprehension [os.path.join(root, file) for root, dirs, files in os.walk(path) for file in files] creates a new list containing the full paths of all files in the specified directory and its subdirectories.
['/path/to/directory/subfolder1','/path/to/directory/subfolder1'/file-in-subfolder1.txt,
 '/path/to/directory/subfolder2','/path/to/directory/file1.txt',
 '/path/to/directory/file2.txt','/path/to/directory/file3.doc']

Using the glob module:

The glob module in Python provides a function, glob.glob(), for file path expansion using wildcards. It allows you to search for files matching a specified pattern, making it useful for tasks like listing files with specific extensions in a directory.

While the glob function may be used to list all files in a directory, it is particularly useful if you only want to list files from a specific type (e.g., all .txt files)

import glob

path = "/path/to/directory"
file_type = "*.txt"

# List all files with the specified file type
files = glob.glob(os.path.join(path, file_type))
print(files)

How does it work:

  • glob.glob(os.path.join(path, file_type)): This function from the glob module returns a list of pathnames matching the specified pattern (*.txt in this case).
  • The resulting list files will contain the paths of all files in the specified directory that match the given file type.

Output

['file1.txt','file2.txt']

Other articles