'Extracting the name of a full file path [duplicate]

I'm trying to get the files name for a path. But every time I run this code below I only get the name of the parent folder instead

    for file in files:
        if file.endswith("png") or file.endswith("jpg"):
            path = os.path.join(root,file)
            print(path)
            label = os.path.basename(os.path.dirname(path))
            print(label)

I get these results :

D:\AI\Deep learning\face generator\images\chris evans 1.jpg
images
D:\AI\Deep learning\face generator\images\chris evans 2.jpg
images

and so on

My expected results are

D:\AI\Deep learning\face generator\images\chris evans 1.jpg
chris evans 1.jpg
D:\AI\Deep learning\face generator\images\chris evans 2.jpg
chris evans 2.jpg


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source