Home The Quantizer> Python> Python: How to Get File Names in a Directory

Python: How to Get File Names in a Directory

TLDR:

from pathlib import Path

directory_path = Path(os.getcwd()) # getting the current directory for this example, use what ever dir you are interested in
file_list = []
# create the list of files in directory
for file in directory_path.iterdir():
    if file.is_file():
        file_list.append(file.name)

# print out the file names in the directory
print("Filenames in the directory:")
for filename in file_list:
    print(filename)

Method 1: os.listdir()

The os module provides a simple yet effective way to retrieve filenames from a directory. The os.listdir() function returns a list containing the names of files and directories in the specified directory.

import os

directory_path = '/path/to/your/directory'
file_list = os.listdir(directory_path)

print("Filenames in the directory:")
for filename in file_list:
    print(filename)

If you want just the files you will have to get a little fancier, in the below example we added a conditional check to add the files to a list and ignored directories.

import os

directory_path = os.getcwd() # getting the current directory for this example, use what ever dir you are interested in
file_list = os.listdir(directory_path) 
print("Files in the directory:")
files = []
for filename in os.listdir(directory_path):
    file_path = os.path.join(directory_path, filename)
    if os.path.isfile(file_path):
        files.append(filename)
print(files)

Method 2: glob.glob()

If you know part of the file name you are looking then glob.glob() may be something to check out. The glob.glob() function allows you to search for files using wildcards.

import glob

directory_path = os.getcwd() # getting the current directory for this example, use what ever dir you are interested in
file_list = glob.glob(directory_path + '/*.txt')

print("Text files in the directory:")
for filename in file_list:
    print(filename)

Here, the example retrieves only text files by specifying the ‘*.txt’ wildcard. Adjust the pattern based on your specific needs.

Method 3: pathlib.Path()

The pathlib module, introduced in Python 3.4, provides a modern and object-oriented approach to file system operations. The Path class is particularly useful for obtaining filenames.

from pathlib import Path

directory_path = Path(os.getcwd()) # getting the current directory for this example, use what ever dir you are interested in
file_list = []
# create the list of files in directory
for file in directory_path.iterdir():
    if file.is_file():
        file_list.append(file.name)

# print out the file names in the directory
print("Filenames in the directory:")
for filename in file_list:
    print(filename)

This method is clean, efficient, and allows for more expressive file filtering using is_file() and has other methods you might like in the Path class.

Conclusion: Retrieving filenames from a directory is a common task in file manipulation, and Python offers several methods to achieve this goal. Depending on your specific requirements, you can choose between the simplicity of os.listdir(), the flexibility of glob.glob(), or the modern approach using pathlib.Path(). Experiment with these methods to find the one that best for your Python file-handling capabilities.