Sunday, February 24, 2013

Rename bunch of files with Python

#!/usr/bin/env python                                                                                                                                                                                                                       

import os, re, commands

fileNameRegEx = '^([0-9])+'
pattern = re.compile( fileNameRegEx )

filesRenamed = 0

for f in os.listdir( '.' ):
    match = pattern.search( f )
    if match:
        newFileName = "{0}.regex".format( match.group(0) )
print "Renaming {0} to {1}".format( f, newFileName )
statusTuple = commands.getstatusoutput( 'mv {0} {1}'.format( f, newFileName ) )
if (statusTuple[0] == 0): filesRenamed += 1

print "Total files renamed {0}".format ( filesRenamed )


No comments:

Post a Comment