Easily Get All Files in a Directory: Python Explained

Easily Get All Files in a Directory: Python Explained

MoeNagy Dev

Getting All Files in a Directory with Python

Importance of Listing Files in a Directory

Understanding the file structure of your project is crucial for effective file management and automation. Being able to list the files in a directory can help you:

  • Understand your project's file organization: By listing the files in a directory, you can quickly see what files are present, their names, and their organization within the directory structure.
  • Automate file-related tasks: Listing files programmatically allows you to perform various tasks, such as file backups, media file organization, or source code analysis, in an automated and efficient manner.
  • Analyze file contents and metadata: Once you have a list of files, you can further process the information, such as file size, modification time, or other metadata, to gain insights about your project.

Basic File Handling in Python

Before we dive into listing files in a directory, let's quickly review the basics of file handling in Python.

Opening and Closing Files

In Python, you can open a file using the open() function. The basic syntax is:

file = open("filename.txt", "r")
# Perform operations on the file

The second argument in the open() function specifies the mode, such as "r" for reading, "w" for writing, or "a" for appending.

It's important to close the file after you're done with it to ensure that any changes are saved and system resources are properly released.

Reading and Writing File Contents

Once a file is open, you can read its contents using the read() method:

file = open("filename.txt", "r")
content =

To write to a file, you can use the write() method:

file = open("filename.txt", "w")
file.write("This is some content to be written to the file.")

Listing Files in a Directory

Now, let's explore how to list the files in a directory using Python.

Using the os Module

The os module in Python provides a set of functions for interacting with the operating system, including file and directory management. To list the files in a directory, we'll use the os.listdir() function.

import os
directory = "/path/to/directory"
files = os.listdir(directory)

This will print a list of all the files and directories within the specified directory.

Note that os.listdir() returns the names of the files and directories, but not their full paths. If you need the full paths, you can combine os.listdir() with os.path.join():

import os
directory = "/path/to/directory"
file_paths = [os.path.join(directory, filename) for filename in os.listdir(directory)]

This will give you a list of full file paths, including the directory and the filename.

Handling Relative and Absolute Paths

When working with file paths, you can use either relative or absolute paths. Relative paths are based on the current working directory, while absolute paths specify the full path from the root directory.

To get the current working directory, you can use os.getcwd():

import os
current_dir = os.getcwd()

You can then use this information to construct relative or absolute paths as needed.

Filtering Files by Extension

Often, you may want to list only the files with a specific extension, such as .txt or .py. You can achieve this using various techniques.

Checking File Extensions

One way to filter files by extension is to check the file extension using string operations:

import os
directory = "/path/to/directory"
txt_files = [f for f in os.listdir(directory) if f.endswith(".txt")]

This uses a list comprehension to create a new list containing only the files with the .txt extension.

Alternatively, you can use the os.path.splitext() function to extract the file extension:

import os
directory = "/path/to/directory"
py_files = [f for f in os.listdir(directory) if os.path.splitext(f)[1] == ".py"]

This approach separates the filename and the extension, allowing you to check the extension directly.

Recursively Traversing Subdirectories

If your project has a complex directory structure with subdirectories, you may want to recursively list all the files in the entire directory tree. The os.walk() function can help you with this task.

import os
directory = "/path/to/directory"
for root, dirs, files in os.walk(directory):
    for file in files:
        print(os.path.join(root, file))

The os.walk() function yields three values for each directory it traverses:

  1. root: The current directory being processed.
  2. dirs: A list of subdirectories in the current directory.
  3. files: A list of files in the current directory.

By iterating over the files list, you can access the full path of each file in the directory tree.

Sorting and Organizing File Lists

Once you have a list of files, you may want to sort or organize them in a specific way. Python's built-in sorted() function can help with this.

Alphabetical Sorting

To sort the file list alphabetically, you can use the sorted() function:

import os
directory = "/path/to/directory"
files = sorted(os.listdir(directory))

This will sort the file list in alphabetical order.

Sorting by File Size or Modification Time

You can also sort the file list based on file size or modification time. To do this, you can provide a custom key function to the sorted() function.

import os
directory = "/path/to/directory"
files = sorted(os.listdir(directory), key=lambda x: os.path.getsize(os.path.join(directory, x)), reverse=True)

This will sort the file list in descending order by file size.

To sort by modification time, you can use os.path.getmtime() instead of os.path.getsize():

import os
from datetime import datetime
directory = "/path/to/directory"
files = sorted(os.listdir(directory), key=lambda x: os.path.getmtime(os.path.join(directory, x)), reverse=True)

This will sort the file list in descending order by modification time.

Working with File Metadata

In addition to the file names and paths, you may also want to retrieve information about the files, such as their size and modification time. Python provides functions to access this metadata.

Retrieving File Size and Modification Time

You can use the os.path.getsize() function to get the size of a file, and os.path.getmtime() to get the last modification time.

import os
from datetime import datetime
directory = "/path/to/directory"
filename = "example.txt"
file_path = os.path.join(directory, filename)
file_size = os.path.getsize(file_path)
file_mtime = os.path.getmtime(file_path)
print(f"File size: {file_size} bytes")
print(f"Last modified: {datetime.fromtimestamp(file_mtime)}")

This will print the file size in bytes and the last modification time of the file.

Formatting File Size and Time Information

To make the file size and time information more readable, you can format them accordingly.

import os
from datetime import datetime
directory = "/path/to/directory"
filename = "example.txt"
file_path = os.path.join(directory, filename)
file_size = os.path.getsize(file_path)
file_mtime = os.path.getmtime(file_path)
# Format file size
if file_size < 1024:
    file_size_str = f"{file_size} bytes"
elif file_size < 1024 * 1024:
    file_size_str = f"{file_size / 1024:.2f} KB"
    file_size_str = f"{file_size / (1024 * 1024):.2f} MB"
# Format modification time
file_mtime_str = datetime.fromtimestamp(file_mtime).strftime("%Y-%m-%d %H:%M:%S")
print(f"File size: {file_size_str}")
print(f"Last modified: {file_mtime_str}")

This will print the file size in a more human-readable format (bytes, KB, or MB) and the modification time in a formatted date and time string.

Handling Errors and Edge Cases

When working with file operations, it's important to handle potential errors and edge cases gracefully. Python's built-in OSError exception can help with this.

import os
directory = "/path/to/directory"
    files = os.listdir(directory)
    for file in files:
        file_path = os.path.join(directory, file)
        file_size = os.path.getsize(file_path)
        print(f"File: {file}, Size: {file_size} bytes")
except OSError as e:
    print(f"Error: {e}")
    print("Unable to access the directory or retrieve file information.")

In this example, we wrap the file listing and file size retrieval operations in a try-except block to catch any OSError exceptions that may occur, such as when the directory is not accessible or a file cannot be read.

By handling these exceptions, you can provide a more graceful error message instead of letting the program crash.

Practical Applications and Use Cases

Now that you have a solid understanding of listing files in a directory, let's explore some practical applications and use cases.

File Backup and Synchronization

One common use case is to create file backups or synchronize files between different locations. By listing the files in a directory, you can identify which files need to be backed up or synchronized.

import os
import shutil
source_dir = "/path/to/source/directory"
backup_dir = "/path/to/backup/directory"
for filename in os.listdir(source_dir):
    src_path = os.path.join(source_dir, filename)
    dst_path = os.path.join(backup_dir, filename)
    shutil.copy2(src_path, dst_path)
    print(f"Backed up: {filename}")

This example copies all the files from the source_dir to the backup_dir directory, effectively creating a backup of the files.

Media File Organization

Another use case is to organize media files (e.g., photos, videos) based on their file extensions or metadata. By listing the files in a directory, you can sort and move them to appropriate subdirectories.

import os
import shutil
media_dir = "/path/to/media/directory"
photo_dir = "/path/to/photos/directory"
video_dir = "/path/to/videos/directory"
for filename in os.listdir(media_dir):
    src_path = os.path.join(media_dir, filename)
    if filename.endswith(".jpg") or filename.endswith(".png"):
        dst_path = os.path.join(photo_dir, filename)
    elif filename.endswith(".mp4") or filename.endswith(".mov"):
        dst_path = os.path.join(video_dir, filename)
    shutil.move(src_path, dst_path)
    print(f"Moved: {filename}")

This example sorts the media files in the media_dir directory based on their file extensions, moving the image files to the photo_dir and the video files to the video_dir directory.

Source Code Analysis and Project Management

Listing files in a directory can also be useful for source code analysis and project management. You can use file listings to:

  • Identify the files that make up a software project
  • Analyze the file structure and organization
  • Generate reports on file sizes, modification times, and other metadata

This information can help you better understand and manage your software projects.

Intermediate Python Concepts

Classes and Object-Oriented Programming (OOP)

In Python, classes are the fundamental building blocks of object-oriented programming. They allow you to create custom data types with their own attributes and methods. Here's an example of a simple Car class:

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
    def start(self):
        print(f"The {self.year} {self.make} {self.model} has started.")
    def stop(self):
        print(f"The {self.year} {self.make} {self.model} has stopped.")

In this example, the Car class has three attributes (make, model, and year) and two methods (start() and stop()). The __init__() method is a special method that is automatically called when you create a new instance of the Car class.

You can create instances of the Car class like this:

my_car = Car("Toyota", "Corolla", 2015)
my_car.start()  # Output: The 2015 Toyota Corolla has started.
my_car.stop()   # Output: The 2015 Toyota Corolla has stopped.

OOP also supports inheritance, which allows you to create new classes based on existing ones. Here's an example of a ElectricCar class that inherits from the Car class:

class ElectricCar(Car):
    def __init__(self, make, model, year, battery_capacity):
        super().__init__(make, model, year)
        self.battery_capacity = battery_capacity
    def charge(self):
        print(f"The {self.year} {self.make} {self.model} is charging.")

The ElectricCar class inherits the make, model, and year attributes, as well as the start() and stop() methods, from the Car class. It also adds a new attribute (battery_capacity) and a new method (charge()).

my_electric_car = ElectricCar("Tesla", "Model S", 2020, 100)
my_electric_car.start()  # Output: The 2020 Tesla Model S has started.
my_electric_car.charge() # Output: The 2020 Tesla Model S is charging.

Modules and Packages

In Python, modules are single files containing code, while packages are collections of related modules. Modules allow you to organize your code and make it reusable across different projects.

Here's an example of a simple module called

def add(a, b):
    return a + b
def subtract(a, b):
    return a - b
def multiply(a, b):
    return a * b
def divide(a, b):
    return a / b

You can then import and use the functions from this module in another Python file:

from math_functions import add, subtract
print(add(2, 3))  # Output: 5
print(subtract(5, 3))  # Output: 2

Packages, on the other hand, allow you to group related modules together. For example, you could create a math package with separate modules for different types of mathematical operations, such as,, and


You can then import the modules from the math package like this:

from math.arithmetic import add, subtract
from math.geometry import calculate_area
from math.statistics import mean, median

Exceptions and Error Handling

In Python, exceptions are a way to handle errors that occur during program execution. You can use try-except blocks to catch and handle exceptions.

Here's an example of how to handle a ZeroDivisionError:

def divide(a, b):
        result = a / b
        print(f"The result is: {result}")
    except ZeroDivisionError:
        print("Error: Cannot divide by zero.")
divide(10, 2)  # Output: The result is: 5.0
divide(10, 0)  # Output: Error: Cannot divide by zero.

You can also use the finally clause to execute code regardless of whether an exception was raised or not:

def open_file(filename):
        file = open(filename, 'r')
        content =
    except FileNotFoundError:
        print(f"Error: {filename} not found.")

Additionally, you can define your own custom exceptions by creating a new class that inherits from the Exception class:

class InvalidInputError(Exception):
def calculate_area(shape, *args):
    if shape == 'rectangle':
        length, width = args
        return length * width
    elif shape == 'circle':
        radius, = args
        return 3.14 * radius ** 2
        raise InvalidInputError("Invalid shape provided.")
    print(calculate_area('rectangle', 5, 10))  # Output: 50
    print(calculate_area('circle', 3))  # Output: 28.26
    print(calculate_area('triangle', 3, 4))  # Raises InvalidInputError
except InvalidInputError as e:

File I/O and Paths

Python provides built-in functions and modules for working with files and file paths. Here's an example of how to read and write to a file:

# Writing to a file
with open('example.txt', 'w') as file:
    file.write("Hello, World!\n")
    file.write("This is a sample text file.")
# Reading from a file
with open('example.txt', 'r') as file:
    content =
    print(content)  # Output: Hello, World!\nThis is a sample text file.

The with statement is used to ensure that the file is properly closed after the operations are complete, even if an exception is raised.

You can also use the os module to work with file paths and directories:

import os
# Get the current working directory
current_dir = os.getcwd()
# Join paths
file_path = os.path.join(current_dir, 'example', 'file.txt')
# Check if a file or directory exists
if os.path.exists(file_path):
    print("File exists.")
    print("File does not exist.")


In this tutorial, you've learned about several intermediate-level Python concepts, including:

  • Classes and Object-Oriented Programming (OOP)
  • Modules and Packages
  • Exceptions and Error Handling
  • File I/O and Paths

These concepts are essential for building more complex and robust Python applications. By understanding and applying these techniques, you can write cleaner, more maintainable, and more versatile code.

Remember, the best way to improve your Python skills is to practice and experiment with these concepts. Try to implement them in your own projects and continuously challenge yourself to learn new things.

Happy coding!

MoeNagy Dev