How to list directory tree structure in python?


While working with file systems and directories in Python, understanding the directories’ structure and their contents is essential for efficient file management and organization. Python makes available various methods and libraries that allow you to list the directory tree structure, including all subdirectories and files, in a comprehensive and exhaustive manner. Whether you're organizing files, analyzing directory structures, or performing data processing tasks, knowing how to list the directory tree structure in Python will significantly enhance your file handling capabilities.

In this extensive article, we will explore different methods to list the directory tree structure in Python. We will also make available step−by−step explanations and code examples to walk you through the process. Whether you prefer using the "os" module, the "pathlib" module, or third−party libraries, this guide will equip you with the tools to navigate directory structures and access valuable information about files and directories.

Let us get started on this path of directory tree exploration with Python and unlock the secrets of listing directory structures!

Using os.walk() for Recursive Directory Traversal

The "os.walk()" function is a powerful tool for traversing directory trees in Python. It allows you to list all directories and files in a given starting directory and its subdirectories recursively.

Example

  • In the code below, we import the "os" module, which provides functions for interacting with the operating system, including directory operations.

  • The "list_directory_tree_with_os_walk()" function takes the "starting_directory" as input and lists the entire directory tree structure using "os.walk()".

  • During the iteration, "os.walk()" returns three values: the root path, a list of directories in the current directory, and a list of files in the current directory.

  • We use a "for" loop to iterate over the root, directories, and files obtained from "os.walk()".

  • For each iteration, we print the current directory path ("root") and list all files in that directory.

import os

def list_directory_tree_with_os_walk(starting_directory):
    for root, directories, files in os.walk(starting_directory):
        print(f"Directory: {root}")
        for file in files:
            print(f"  File: {file}")

Utilizing pathlib.Path for Directory Tree Traversal

The "pathlib" module offers a more modern and object−oriented way to traverse directory trees in Python. The "rglob()" method recursively lists all files in the specified directory and its subdirectories.

Example

  • In this example, we import the "Path" class from the "pathlib" module, which represents a file system path.

  • The "list_directory_tree_with_pathlib()" function takes the "starting_directory" as input and lists the entire directory tree structure using "pathlib".

  • We create a "Path" object with "Path(starting_directory)", where "starting_directory" is the input directory.

  • We use the "rglob('*')" method on the "Path" object to recursively list all files and directories in the specified directory and its subdirectories.

  • During the iteration, we use "file_path.is_file()" to check if the current item is a file and "file_path.is_dir()" to check if it's a directory.

  • We then print the file or directory path accordingly.

from pathlib import Path

def list_directory_tree_with_pathlib(starting_directory):
    path_object = Path(starting_directory)
    for file_path in path_object.rglob('*'):
        if file_path.is_file():
            print(f"File: {file_path}")
        elif file_path.is_dir():
            print(f"Directory: {file_path}")

Displaying Indentation for Better Structure

To enhance the readability of the directory tree structure, we can display indentation based on the depth of subdirectories using recursion.

Example

  • In the code below, we define the "list_directory_tree_with_indentation()" function, which lists the entire directory tree structure with indentation for better readability.

  • The function takes the "directory" and "indent" as inputs, where "directory" is the starting directory and "indent" is the current indentation level.

  • We use "os.listdir(directory)" to obtain a list of items (files and directories) in the specified directory.

  • For each item, we construct its full path with "os.path.join(directory, item)".

  • If the item is a file, we print its name with indentation based on the current level ("{' ' * indent}").

  • If the item is a directory, we print its name with indentation and recursively call the "list_directory_tree_with_indentation()" function with the subdirectory path and an incremented "indent" value.

import os

def list_directory_tree_with_indentation(directory, indent=0):
    for item in os.listdir(directory):
        item_path = os.path.join(directory, item)
        if os.path.isfile(item_path):
            print(f"{'  ' * indent}File: {item}")
        elif os.path.isdir(item_path):
            print(f"{'  ' * indent}Directory: {item}")
            list_directory_tree_with_indentation(item_path, indent+1)

Excluding Certain Directories from the Listing

In some cases, you might want to exclude specific directories from the listing. Python provides options to filter out unwanted directories using various techniques.

Example

  • The "list_directory_tree_exclude_directories()" function allows you to list the directory tree structure while excluding specific directories from the listing.

  • It takes the "directory" and "exclude_dirs" (a list of directories to exclude) as inputs.

  • We use "os.listdir(directory)" to obtain a list of items (files and directories) in the specified directory.

  • For each item, we construct its full path with "os.path.join(directory, item)".

  • If the item is a file, we print its name as usual.

  • If the item is a directory, we check if its name is present in the "exclude_dirs" list using "item not in exclude_dirs".

  • If it's not in the list, we print its name and recursively call the "list_directory_tree_exclude_directories()" function with the subdirectory path and the same "exclude_dirs" list.

import os

def list_directory_tree_exclude_directories(directory, exclude_dirs=[]):
    for item in os.listdir(directory):
        item_path = os.path.join(directory, item)
        if os.path.isfile(item_path):
            print(f"File: {item}")
        elif os.path.isdir(item_path):
            if item not in exclude_dirs:
                print(f"Directory: {item}")
                list_directory_tree_exclude_directories(item_path, exclude_dirs)

Displaying File Sizes and Last Modified Dates

To obtain additional information about files in the directory tree, such as file sizes and last modified dates, we can use the "os.path" module and the "os.stat()" function.

Example

  • The "list_directory_tree_with_file_info()" function lists the directory tree structure and includes additional information about files, such as file sizes and last modified dates.

  • It takes the "directory" as input.

  • We use "os.listdir(directory)" to obtain a list of items (files and directories) in the specified directory.

  • For each item, we construct its full path with "os.path.join(directory, item)".

  • If the item is a file, we use "os.path.getsize(item_path)" to get the file size in bytes and "os.path.getmtime(item_path)" to get the last modified timestamp.

  • We convert the timestamp to a human−readable format using "datetime.fromtimestamp()".

  • If the item is a directory, we print its name and recursively call the "list_directory_tree_with_file_info()" function with the subdirectory path to obtain file information for all nested directories and files.

import os
from datetime import datetime

def list_directory_tree_with_file_info(directory):
    for item in os.listdir(directory):
        item_path = os.path.join(directory, item)
        if os.path.isfile(item_path):
            file_size = os.path.getsize(item_path)
            last_modified = datetime.fromtimestamp(os.path.getmtime(item_path))
            print(f"File: {item} - Size: {file_size} bytes - Last Modified: {last_modified}")
        elif os.path.isdir(item_path):
            print(f"Directory: {item}")
            list_directory_tree_with_file_info(item_path)

In this comprehensive article, we've explored various methods to list the directory tree structure in Python. By using techniques such as "os.walk()", "pathlib", recursion, and filtering, you can navigate directory structures efficiently and access valuable information about files and directories. Whether you need a simple listing or more detailed file information, Python has a number of tools to handle directory trees with ease. Mastering these methods will empower you to perform complex file management tasks, optimize data processing, and organize your projects effectively.

Updated on: 11-Sep-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements