Wednesday, May 6, 2009

Some times you just have to rename files.

So I needed to rename a bunch of files. I needed to add the size of the file as a postfix. Same name just an adition of 16x16 which when doing it by hand is tedious. So this little script was created. You have to dump it into the same folder as the files you are working with, and you have to put the path in walkPath. I could have it pull the current directory from the os but that would take work.

   import os

   walkPath = r"some disk path"

   for d in os.walk(walkPath):
      dirpath, dirnames, filenames = d
      for a in filenames:
         if a.endswith(".py"):
            pass
         else:
            print a
            aSplit = a.split(".")
            os.rename(a,aSplit[0]+"16x16."+aSplit[1])

1 comment:

Mel said...

That's awesome! Simple, gets the job done.

I like the tags you used for this post, they're so appropriate.