change extensions

David Wilson davemw at gmail.com
Mon Apr 4 19:42:53 EDT 2005


Bob Then wrote:
> how can i change all files from one extension to another within a
direcory?

This should work:

import os

def change_exts(suffix, new_suffix, dir_name):
    for name in os.listdir(dir_name):
        if name.endswith(suffix):
            old_pathname = os.path.join(dir_name, name)
            new_pathname = old_pathname[:-len(suffix)] + new_suffix
            os.rename(old_pathname, new_pathname)



David.




More information about the Python-list mailing list