尝试从 glob 列表中打印词干(基本名称 w/o 扩展名)

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

我正在尝试打印 glob 列表中每个文件的仅词干。文件名是人名,所以我只想显示他们的名字,而不显示完整路径和扩展名。有什么建议么?我看过 os.path,但 pathlib.stem 似乎是最干净的。

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

提前致谢。

使用 pathlibglob and then get the stem. pathlib.Path.stem 给你没有扩展名的文件名。

from pathlib import Path

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