File: thumbspage/examples/dynamiclayout/make-narrow-labels.py

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

import os, mimetypes

mydir = os.path.dirname(__file__)
os.chdir(mydir)

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('make-narrow-labels.py changed:', nchanged)



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