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.