Back to Python
2025-11-226 min read

List Directories and Files in Python

Learn List Directories and Files in Python step by step with clear examples and exercises.

Title: List Directories and Files in Python: A full guide

Why This Matters

In programming, managing files and directories is a fundamental task that every developer encounters. Python provides several built-in functions to work with directories and files, making it an essential skill for any Python programmer. Understanding how to list directories and files in Python can help you navigate the file system, automate tasks, and even debug issues more effectively.

Prerequisites

Before diving into listing directories and files in Python, you should be familiar with the following concepts:

  1. Basic Python syntax (variables, data types, operators)
  2. Control structures (if-else, for loops, while loops)
  3. Functions and modules
  4. Exception handling
  5. Understanding file paths and directories in your operating system

Core Concept

Python provides several built-in functions to work with directories and files. In this guide, we will focus on the os module, which contains platform-independent ways of using operating system dependent functionality. Specifically, we will use the listdir() function from the os module to list all files and directories within a given path.

Listing Directories and Files

To list all files and directories in a specific directory, you can use the following code:

import os

path = "/path/to/directory"
files_and_dirs = os.listdir(path)

for item in files_and_dirs:
print(item)

Replace /path/to/directory with the path you want to list. The os.listdir() function returns a list of all entries (files, directories, and hidden files starting with a dot) within the specified directory. The loop then iterates through this list and prints each item.

Filtering Files and Directories

If you only want to list files or directories, you can use Python's built-in functions os.path.isfile() and os.path.isdir(). Here's an example:

import os
import os.path

path = "/path/to/directory"
files_only = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
directories_only = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]

print("Files:", files_only)
print("Directories:", directories_only)

This code creates two separate lists: one for files and another for directories. The os.path.join() function is used to concatenate the directory path with each item from os.listdir().

Handling Exceptions

When working with file paths, it's important to handle exceptions that might occur due to invalid paths or permissions issues. You can use a try-except block to catch and handle these exceptions:

import os

try:
path = "/path/to/directory"
files_and_dirs = os.listdir(path)
for item in files_and_dirs:
print(item)
except FileNotFoundError as e:
print("Error:", e)

In this example, a FileNotFoundError exception is caught and printed if the specified path does not exist.

Worked Example

Let's list all files and directories in the current working directory and filter them to show only directories:

import os
import os.path

List all entries (files, directories, hidden files)

all_entries = os.listdir(".")

print("All Entries:", all_entries)

Filter to show only directories

directories = [d for d in all_entries if os.path.isdir(os.path.join(".", d))]

print("Directories Only:", directories)


When you run this code, it will output the list of all entries and then list only the directories within the current working directory.

Common Mistakes

  1. Not specifying a path: Make sure to provide a valid path when using os.listdir(). If no path is provided, it defaults to the current working directory.
  2. Forgetting to join the path and item: When using os.path.isfile() or os.path.isdir(), always use os.path.join(path, item) to ensure the correct path is used.
  3. Not handling hidden files: By default, os.listdir() includes hidden files (files starting with a dot). If you want to exclude them, you can use list comprehension with a conditional statement to filter them out or use os.scandir() and check the .name attribute of each entry.
  4. Using absolute vs relative paths: Be aware of the difference between absolute and relative paths. Absolute paths start from the root directory, while relative paths are relative to the current working directory.
  5. Not handling exceptions: When working with file paths, it's important to handle exceptions that might occur due to invalid paths or permissions issues.

Practice Questions

  1. Write a script that lists all text files (files with .txt extension) in the current working directory.
  2. Create a function that takes a directory path as an argument and returns a list of all subdirectories within that directory.
  3. Write a script that finds all Python scripts (files with .py extension) within a specific directory and its subdirectories.
  4. Given a directory containing both files and directories, write a script to count the number of each (files and directories).
  5. Write a script that lists all files modified in the last 24 hours in the current working directory.
  6. Implement a function to move a file from one directory to another while handling exceptions related to file existence or permissions.
  7. Write a script that recursively finds all media files (images, videos, and audio files) within a specific directory and its subdirectories.
  8. Create a function that takes a list of directories as input and returns the total number of files and directories across all provided directories.

FAQ

How can I list only the files in a directory?

  • You can use Python's built-in functions os.listdir() and os.path.isfile() to filter out directories and only show files.

What is the difference between absolute and relative paths in Python?

  • Absolute paths start from the root directory, while relative paths are relative to the current working directory.

How can I handle hidden files when listing directories and files?

  • By default, os.listdir() includes hidden files (files starting with a dot). If you want to exclude them, you can use list comprehension with a conditional statement to filter them out or use os.scandir() and check the .name attribute of each entry.

Can I use other modules besides the os module to work with directories and files in Python?

  • Yes, there are several other modules like glob, shutil, and os.path that can help you work with directories and files in Python. However, the os module is a good starting point for most basic file system operations.

How can I list all files modified in the last 24 hours in the current working directory?

  • You can use the os and time modules to list all files modified within the last 24 hours. First, get the timestamp of 24 hours ago, then loop through the files and directories, checking their modification times against the calculated timestamp. If a file was modified within the last 24 hours, add it to a list and print the list at the end.

How can I move a file from one directory to another while handling exceptions related to file existence or permissions?

  • Use a try-except block to handle potential exceptions when moving the file. If the destination file already exists, you might want to overwrite it, rename it, or prompt the user for action. Also, make sure that the source and destination directories have proper permissions to read and write files.
List Directories and Files in Python | Python | XQA Learn