【发布时间】:2020-11-24 06:27:30
【问题描述】:
使用 Python 3.7.7,我想获取所有不以 *_mask.tif 结尾的图像的列表。
路径中有以*.tif 和*_mask.tif 结尾的图像。但是下面的代码会返回所有这些。
# Read all the brain images (those that don't end with _mask.tif).
def brain_images_list(path):
if not isinstance(path, str):
raise TypeError('path must be a string')
if not os.path.exists(path):
raise ValueError('path must exist: ', path)
if not os.path.isdir(path):
raise ValueError('path must be a directory: ', path)
# Save current directory.
current_dir = os.getcwd()
# Change current directory to the one we want to look for.
os.chdir(path)
# Get brain images list
brain_images_lst = glob.glob('*.tif')
# Restore directory
os.chdir(current_dir)
return brain_images_lst
我该怎么做?
【问题讨论】:
-
在
brain_images_lst = glob.glob('*.tif')你得到了所有的图像。你需要过滤掉那些你不想要的。 -
是的,我知道我必须做什么,我的问题是我不知道该怎么做。
-
brain_images_lst = [image for image in brain_images_lst if not image.endswith("_mask.tif")]? -
这能回答你的问题吗? glob exclude pattern
标签: python