File: thumbspage/examples/dynamiclayout/

# Q-and-d script to shorten filenames to,, ... in dynamic narrow gallery.

import os, mimetypes

mydir = os.path.dirname(__file__)

nchanged = 0
gallery = 'Demo-Narrow-Filename-Labels'
names = sorted(os.listdir(gallery))    # match original

for (ix, name) in enumerate(names):
    mimetype = mimetypes.guess_type(name)[0]
    if mimetype != None and mimetype.split('/')[0] == 'image':
        nchanged += 1
        os.rename(os.path.join(gallery, name), 
                  os.path.join(gallery, str(ix).zfill(3) + os.path.splitext(name)[1]) )

        # [2.3] copy .note files too, else only appear in wide demo
        note = os.path.join(gallery, name) + '.note'
        if os.path.exists(note):
            os.rename(note, note.replace(os.path.splitext(name)[0], str(ix).zfill(3)))          

print(' changed:', nchanged)

[Home page] Books Code Blog Python Author Train Find ©M.Lutz