• Javascript
  • Python
  • Go

Python Directory Tree Listing

Python is a powerful and versatile programming language that is widely used in various industries such as web development, data science, and...

Python is a powerful and versatile programming language that is widely used in various industries such as web development, data science, and artificial intelligence. One of the key features of Python is its ability to manipulate and organize data efficiently. In this article, we will explore how Python can be used to create a directory tree listing.

A directory tree is a hierarchical structure that represents the organization of files and folders in a computer system. It is commonly used to navigate and manage files in a structured manner. With Python, we can generate a directory tree listing that displays the files and folders in a visually appealing and organized manner.

To begin, we will need to import the os module, which provides a wide range of functions for interacting with the operating system. We will also use the os.path module, which contains functions for manipulating file and directory paths.

import os

import os.path

Next, we will define a function called "listdir" that will take a directory path as an argument. This function will use the os.listdir() method to get a list of all the files and folders in the specified directory. It will then loop through the list and print out the name of each file and folder.

def listdir(path):

files = os.listdir(path)

for file in files:

print(file)

Now, let's create a variable called "path" and assign it the path of the directory we want to list. For example, if we want to list the files and folders in the "Documents" folder, we can use the following code:

path = "C:/Users/Username/Documents"

We can also use the os.path.join() method to join the directory path with the filenames, making it easier to navigate through the directory tree.

def listdir(path):

files = os.listdir(path)

for file in files:

print(os.path.join(path, file))

Finally, we will call the listdir() function and pass in the "path" variable as an argument.

listdir(path)

When we run this code, we will get a list of all the files and folders in the "Documents" directory, including their full paths. This will give us a directory tree-like structure, with the folders appearing first followed by the files within them.

C:/Users/Username/Documents/Folder1

C:/Users/Username/Documents/Folder2

C:/Users/Username/Documents/Folder3

C:/Users/Username/Documents/File1.txt

C:/Users/Username/Documents/File2.pdf

C:/Users/Username/Documents/File3.py

We can also modify the listdir() function to add more details, such as the size and last modified date of each file. This can be achieved by using the os.path.getsize() and os.path.getmtime() methods.

def listdir(path):

files = os.listdir(path)

for file in files:

full_path = os.path.join(path, file)

size = os.path.getsize(full_path)

modified = os.path.getmtime(full_path)

print(file, "Size:", size, "Last Modified:", modified)

When we run this code, we will get a more detailed directory tree listing, including the file size and last modified date.

Folder1 Size: 500 KB Last Modified: 01/01/2021

Folder2 Size: 1 MB Last Modified: 05/03/2021

Folder3 Size: 2 GB Last Modified: 10/07/2021

File1.txt Size: 100 KB Last Modified: 07/02/2021

File2.pdf Size: 1.5 MB Last Modified: 12/04/2021

File3.py Size: 2 KB Last Modified: 21/09/2021

In conclusion, Python provides us with a simple yet effective way to generate a directory tree listing. With just a few lines of code, we can access and display information about the files and folders in a given directory in a structured and organized manner. This can be useful for managing large amounts of data and navigating through complex file systems. So, the next time you need to list the contents of a directory, remember to use Python for a hassle-free and customizable solution.

Related Articles