How to Get Filenames in Python: Using os, pathlib, and glob with Examples

1. Introduction

Python is highly flexible when it comes to file operations, allowing you to automate many tasks with simple code. In this article, we will introduce several ways to “get filenames” using Python. We will mainly focus on the standard libraries os and pathlib. By reading this article, you will learn how to efficiently handle files in a directory and build scripts that retrieve and filter filenames.

2. Basics of Getting Filenames in Python

Let’s start with the basic methods for getting filenames in Python. By using the os.path.basename() function, you can extract the filename from a given path.

2.1 How to Use os.path.basename()

os.path.basename() is a function that extracts only the filename from a full path. The example below shows how to get a filename from a file path.
import os

file_path = "/user/home/document/file.txt"
file_name = os.path.basename(file_path)
print(file_name)  # Output: "file.txt"

2.2 Getting the Directory Name with os.path.dirname()

Similarly, you can use os.path.dirname() to get the directory name from a file path. This allows you to separate filenames from directory names.
dir_name = os.path.dirname(file_path)
print(dir_name)  # Output: "/user/home/document"
侍エンジニア塾

3. Getting All Filenames in a Directory

To retrieve all files and folders in a directory as a list, os.listdir() is useful. For recursively getting files in subdirectories, os.walk() comes in handy.

3.1 Getting Filenames with os.listdir()

os.listdir() returns all files and folders in the specified directory as a list. It is ideal for simple directory operations.
import os

dir_path = "/user/home/document"
files = os.listdir(dir_path)
print(files)  # Output: ["file1.txt", "file2.txt", "subfolder"]

3.2 Getting Filenames Recursively with os.walk()

If you want to recursively get files including those in subdirectories, os.walk() is the most effective method. The following code lists all files in the specified directory and its subdirectories.
import os

dir_path = "/user/home/document"
for root, dirs, files in os.walk(dir_path):
    for file in files:
        print(os.path.join(root, file))

3.3 Adding Error Handling

Errors may occur if the directory does not exist or if you don’t have access permissions. Therefore, it is recommended to use try-except for exception handling.
dir_path = "/invalid/path"
try:
    files = os.listdir(dir_path)
    print(files)
except FileNotFoundError:
    print(f"{dir_path} not found.")

4. Getting and Changing File Extensions

When retrieving filenames, sometimes you also need to handle extensions. You can use os.path.splitext() to separate or modify file extensions.

4.1 Getting File Extensions with os.path.splitext()

os.path.splitext() returns the filename and extension separately.
import os

file_path = "/user/home/document/file.txt"
file_name, file_ext = os.path.splitext(file_path)
print(file_name)  # Output: "/user/home/document/file"
print(file_ext)   # Output: ".txt"

4.2 Changing File Extensions

To change an extension, simply add a new one to the filename obtained with os.path.splitext().
new_file_path = file_name + ".jpg"
print(new_file_path)  # Output: "/user/home/document/file.jpg"
年収訴求

5. Advanced Filename Retrieval with pathlib

Since Python 3.4, the pathlib module has been extremely useful for working with file paths. pathlib allows you to handle file paths in an object-oriented way, making it more intuitive.

5.1 Getting Filenames with Path.iterdir()

The iterdir() method of pathlib.Path() lets you retrieve file and folder names in a specified directory.
from pathlib import Path

dir_path = Path("/user/home/document")
for item in dir_path.iterdir():
    print(item.name)

5.2 Recursive File Retrieval with rglob()

To recursively retrieve filenames including subdirectories, use the rglob() method. It can also serve as an alternative to os.walk().
for file in dir_path.rglob("*"):
    print(file)

6. Filtering Filenames

If you want to retrieve only filenames that match specific conditions, you can filter them using the glob module or the glob() method of pathlib.

6.1 Filtering by Extension

To retrieve only files with a specific extension, use the wildcard *.
from pathlib import Path

dir_path = Path("/user/home/document")
for file in dir_path.glob("*.txt"):
    print(file)
This code retrieves only .txt files in the specified directory.

7. Conclusion

There are many ways to get filenames in Python, but choosing the right method depends on your use case. From basic operations with the os module to advanced features with pathlib, you can efficiently process files in a directory. In the next article, we will cover advanced file operations such as renaming, moving, and deleting files.
侍エンジニア塾