How to use  pathlib module In Python

  • At first, we imported the pathlib module from Path.
  • Then we pass the directory/folder inside Path() function and used it .glob(‘*.png’) function to iterate through all the images present in this folder.

Python3




# import required module
from pathlib import Path
 
# get the path/directory
folder_dir = 'Gfg images'
 
# iterate over files in
# that directory
images = Path(folder_dir).glob('*.png')
for image in images:
    print(image)


Output:

How to iterate through images in a folder Python?

In this article, we will learn how to iterate through images in a folder in Python. 

Similar Reads

Method 1: Using os.listdir

Example 1: Iterating through .png only...

Method 2: Using  pathlib module

...

Method 3: Using glob.iglob()

...