在 glob 中使用隐式路径名参数

Using implicit path name argument in glob

我正在使用 glob.glob() 读取目录中的所有 png 文件。这就是我现在使用的

for filename in glob.glob('D:\test_files\**\*.png',recursive=True):
    ...

图像文件保存在 test_files 的子文件夹中。该代码将在另外数千行代码中被其他人使用。因此,目录应该在代码的其他地方定义为字符串。我还想通过使用另一个 for 循环来更改 *.png。所以,我需要像

这样的东西
for filename in glob.glob('imagepath'+'extension',recursive=True):
    ...

其中图像路径在代码的其他地方定义,扩展名保存在列表中。我可以这样使用glob.glob()方法吗?

答案是here。我将路径定义为 path = 'D:\test_files\**',并将扩展名保存在列表中,如 imgExtentions =['\*.jpg', '\*.png']。然后我简单地使用 glob:

for extension in imgExtentions:
        for filename in glob.glob(path+extension,recursive=True):