Skip to content

Renaming object files extensions

seanws edited this page Aug 10, 2012 · 1 revision

Python script to rename object files from .lib to .a

#Python version 3 

import os

def renameFilesInDirAndSubs(dir,old_ext,new_ext): 
currDir = os.path.abspath(dir) 

print (currDir + ":")
files = os.listdir(dir)
for name in files:
   file = os.path.join(currDir,name)
   if os.path.isdir(file):
       renameFilesInDirAndSubs(file,old_ext,new_ext)
   else:
       if "." + old_ext in name:
           newName = name.replace("." + old_ext, "." + new_ext)
           newFile = os.path.join(currDir,newName)
           os.rename(file, newFile)
           print (file + " renamed to:  " + newName)

def renameFilesInDir(dir,old_ext,new_ext):
    files = os.listdir(dir)
    for file in files:
        if old_ext in new_ext:
            newfile = file.replace(old_ext,new_ext)
            os.rename(file,newfile)


if __name__ == '__main__':
    renameFilesInDirAndSubs("C:\\boost\\stage\\lib","lib","a")


# In the line above, change the first argument to whatever your boost lib directory is.