Predictive Hacks

How to get all Files from Directories and Subdirectories

Assume that you are in your working directory, and you want to get all the .jpg files from all directories and subdirectories under your current working directory. Let’s see how we can achieve that:

import os
import pandas as pd

# Set your root directory to be the current directory
root = os.getcwd()

image_names = []
paths = []
full_path = []
for path, subdirs, files in os.walk(root):
    for name in files:
        if name.endswith('.jpg'):
            image_names.append(name)
            # the "\\" is because I use windows. It will be "/" for OS and Linux
            paths.append(path.split("\\")[-1])
            full_path.append(os.path.join(path, name))

df = pd.DataFrame({'folder':paths, 'image':image_names, 'full_path':full_path})

Now your df data frame will have 3 columns, the folder, the image that refers to the file name of the images and the full_path of the image files.

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s