def path_iterator(path_name): for fp in os.listdir(path_name): if fp.endswith(".png"): yield fpOr if you want to allow several file types, pass the
endswith()
method a tuple with the extensions.
supported_file_types = '.png', '.jpg', '.tif', '.hdr' # a tuple. def path_iterator(path_name): for fp in os.listdir(path_name): if fp.endswith(supported_file_types): yield fp
Questions?
on 13/04/16, Piotr Arłukowicz asks:Q = Hmmm, how does this work? Any example of application, or some more working code?
A = You might use this to dynamically list the content of a directory of images.
path_name
will refer to a directory (full path to this directory) where you expect to find a collection of images.
import os supported_file_types = '.png', '.jpg', '.tif', '.hdr' # a tuple. def path_iterator(path_name): for fp in os.listdir(path_name): if fp.endswith(supported_file_types): yield fp path_name = "C:/my_docs/some_folder/some_folder" for file_name in path_iterator(path_name): print(os.path.join(path_name, file_name))