Get the list of Files in a Directory with Size using Python


Directories are special kind of nodes in the computer system that contains other nested folders, files, etc. To list down the directories, we need special libraries or modules of Python like os module, glob module, etc. They also provide us the methods to access the size of the directory. The size of the directory is the space occupied by the directory in the computer ROM. In this article, we will explore how to get the list of files in a directory along with the size.

Using OS Module

The Python os module is a powerful tool that allows developers to interact with the underlying operating system. It provides various functions for performing file and directory management tasks, process handling, environment variables, and more. With the os module, you can create, delete, and manipulate files and directories, execute system commands, navigate file paths, and access various system−related information. It bridges your Python code and the underlying operating system, enabling you to build robust, platform−independent applications.

Example

We first imported the s module using the import statement in the following code. Next, we created the function named get_file_sizes, which takes the directory's name and prints all the names of the files and their size. We used the “listdir” to list all the files and directories in the directory. Next, we iterated through the list, and for each iteration, we checked if it was a file if yes, we printed the name and the size using the “getsize” method.

import os
def get_file_sizes(directory):
    files = os.listdir(directory)
    for file in files:
        path = os.path.join(directory, file)
        if os.path.isfile(path):
            size = os.path.getsize(path)
            print(f"{file}: {size} bytes")
directory_path = "files"
get_file_sizes(directory_path)

Output

file1.txt: 12 bytes
file2.txt: 26 bytes
file3.txt: 56 bytes

Using Glob and OS Module

The Python glob module helps search files and directories using wildcard patterns. It allows you to define specific patterns or templates and match them against file or directory names. You can create flexible patterns to find files that meet your criteria using wildcards like "*", "?" and others. With the glob module, you can easily retrieve a list of files and their corresponding paths that match your specified pattern. It provides a convenient and efficient way to perform file searches and manipulations based on customizable patterns.

Example

In the following code, we first imported the glob and the os module using the import statement of Python. Next, we have defined a function named get_files_sizes which takes the directory path as the argument. Next, we used the glob method of the glob module to find all the paths in the directory. We iterated through the list, and if it was a file, we used the “getsize” method of os to determine the size of the files.

import glob
import os
def get_file_sizes(directory):
    file_paths = glob.glob(os.path.join(directory, "*"))
    for path in file_paths:
        if os.path.isfile(path):
            size = os.path.getsize(path)
            file_name = os.path.basename(path)
            print(f"{file_name}: {size} bytes")
directory_path = "files"
get_file_sizes(directory_path)

Output

file1.txt: 12 bytes
file2.txt: 26 bytes
file3.txt: 56 bytes

Using the Path Module of Pathlib Library

The pathlib library in Python is a powerful tool for working with file system paths in an object−oriented manner. It provides a user−friendly and intuitive approach to handling paths, directories, and files. With pathlib, you can easily create, manipulate, and navigate paths using a consistent and expressive syntax. The advantage of using this library is that it eliminates the usage of complex string manipulation to deal with file names.

Example

We first imported the Path module from the pathlib library in the following code. Next, we created a function that takes the directory's name as the argument. We used the Path method to create an object and the iterdir method to iterate through all the directories and folders within the directory. Next, we checked if the element in the list was a directory, and if it s so, we used the stat().st_size method to determine the size.

from pathlib import Path

def get_file_sizes(directory):
    path = Path(directory)
    files = path.iterdir()
    for file in files:
        if file.is_file():
            size = file.stat().st_size
            print(f"{file.name}: {size} bytes")
directory_path = "files"
get_file_sizes(directory_path)

Output

file1.txt: 12 bytes
file2.txt: 26 bytes
file3.txt: 56 bytes

Using Stat Method of OS Module

os module offers another method to determine the file size− stat method. The method helps to retrieve various information about the files and directories. It returns a tuple object that contains various attributes and specified paths, such as file size, permissions, timestamps, and more.

Example

In the following code, we first imported the os module. Next, under the in−built function, we used the scandir method to list all the files and directories present. We iterated through the files, and for each entry, we checked if the element was a file. We used the "stat" method and the st_size attribute to determine the size if it is a file.

import os

def get_file_sizes(directory):
    with os.scandir(directory) as entries:
        for entry in entries:
            if entry.is_file():
                size = entry.stat().st_size
                print(f"{entry.name}: {size} bytes")
directory_path = "files"
get_file_sizes(directory_path)

Output

file1.txt: 12 bytes
file2.txt: 26 bytes
file3.txt: 56 bytes

Conclusion

In this article, we understood how to list the files in a directory and the size using different libraries of Python. Python is a general−purpose scripting language offering various methods to list files. This includes glob, os, pathlib, etc. They are platform−independent codes, enabling the coders to use the same code for multiple applications.

Updated on: 18-Jul-2023

531 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements