Output list of files from slideshow
<p>I have adapted a python script to display a slideshow of images.  The original script can be found at <a href="https://github.com/cgoldberg/py-slideshow" rel="nofollow">https://github.com/cgoldberg/py-slideshow</a></p>

<p>I want to be able to record the filename of each of the images that is displayed so that I may more easily debug any errors (i.e., remove incompatible images).</p>

<p>I have attempted to include a command to write the filename to a text file in the <code>def get_image_paths</code> function.  However, that has not worked.  My code appears below - any help is appreciated.</p>

<pre><code>import pyglet
import os
import random
import argparse

window = pyglet.window.Window(fullscreen=True)

def get_scale(window, image):
    if image.width &gt; image.height:
        scale = float(window.width) / image.width
    else:
        scale = float(window.height) / image.height
    return scale


def update_image(dt):
    img = pyglet.image.load(random.choice(image_paths))
    sprite.image = img
    sprite.scale = get_scale(window, img)
    if img.height &gt;= img.width:
        sprite.x = ((window.width / 2) - (sprite.width / 2))
        sprite.y = 0
    elif img.width &gt;= img.height:
        sprite.y = ((window.height / 2) - (sprite.height / 2))
        sprite.x = 0
    else:
        sprite.x = 0
        sprite.y = 0
     window.clear()

thefile=open('test.txt','w')
def get_image_paths(input_dir='.'):
    paths = []
    for root, dirs, files in os.walk(input_dir, topdown=True):
        for file in sorted(files):
            if file.endswith(('jpg', 'png', 'gif')):
                path = os.path.abspath(os.path.join(root, file))
                paths.append(path)
            thefile.write(file)
    return paths


@window.event()
def on_draw():
   sprite.draw()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('dir', help='directory of images',
                        nargs='?', default=os.getcwd())
    args = parser.parse_args()
    image_paths = get_image_paths(args.dir)
    img = pyglet.image.load(random.choice(image_paths))
    sprite = pyglet.sprite.Sprite(img)
    pyglet.clock.schedule_interval(update_image, 3)
    pyglet.app.run()
</code></pre>

<p>I ended up declaring the random image chosen to a variable that I, then, wrote to a txt file.  Relevant code with changes appears below:</p>

<pre><code>thefile=open('test.txt','w')

def update_image(dt):
pic = random.choice(image_paths)
img = pyglet.image.load(pic)
thefile.write(pic+'\n')
thefile.flush()
sprite.image = img
sprite.scale = get_scale(window, img)
if img.height &gt;= img.width:
    sprite.x = ((window.width / 2) - (sprite.width / 2))
    sprite.y = 0
elif img.width &gt;= img.height:
    sprite.y = ((window.height / 2) - (sprite.height / 2))
    sprite.x = 0
else:
    sprite.x = 0
    sprite.y = 0
window.clear()
</code></pre>

<p>Thank you @furas for pointing me in the right direction regarding log files and flushing the buffer to be certain to capture all instances.</p>

python for-loop pyglet