Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

June 01, 2015

A png iterator generator from a given dirname

def path_iterator(path_name):
    for fp in os.listdir(path_name):
        if fp.endswith(".png"):
            yield fp

Or 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))