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

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

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

    
    
    import pyglet
    import os
    import random
    import argparse
    
    window = pyglet.window.Window(fullscreen=True)
    
    def get_scale(window, image):
        if image.width > 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 >= img.width:
            sprite.x = ((window.width / 2) - (sprite.width / 2))
            sprite.y = 0
        elif img.width >= 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()
    


Answer: System don't have to write to file at once but it can keep text in buffer and
saves when you close file. So probably you have to close file.

Or you can use `thefile.flush()` after every `thefile.write()` to send new
text from buffer to file at once.

