Search code examples
pythonpycharmglobpathlib

Trying to print the stem (basename w/o extension) from a glob list


I'm trying to print the stem-only of each file in a glob list. The filenames are the names of people so I would like to just display their names without full path and extension. Any suggestions? I have looked at os.path but the pathlib.stem seemed the cleanest.

    def listknownfaces(self, instance):
        faceslist = glob.glob('Event_Faces/*.jpg')
        print(faceslist)
        for one_item in faceslist.glob('*.jpg'):
            print(one_item.stem)

Thanks in advance.


Solution

  • Use pathlib to glob and then get the stem. pathlib.Path.stem gives you the name of the file without the extension.

    from pathlib import Path
    
    p = Path("Event_Faces")
    for path in p.glob("*.jpg"):
        print(path.stem)