jump to content

#!/usr/bin/python
"""
Just a small script that renames all the JPEG files
in a folder as 0000.jpg, 0001.jpg etc. Initial order
is retained.
vsbabu AT hotmail DOT com

This is intended as a recipe script, though I use it
way too often to arrange my digital files.

Note that this has possible error conditions that
are not handled at all:
- what if the folder has files already numbered?
- while renaming, it fails if the target file exists
"""
import glob
import string
import os

#get all the jpg file names in the current folder
files = glob.glob("*.jpg") 
#sort the list
files.sort()
count = 0
# and rename each file
for f in files:
    count = count + 1
    n = string.zfill(count,4) + ".jpg"
    print f, n, 
    try:
        os.rename(f, n)
        print
    except:
        print "error: didn't rename"