简单写了一个,记录一下:

#!/usr/bin/python
#-*- encoding:utf-8 -*-
import os

class ChangeExt:
    '''
    Try to change the extension name of the files in bats
    
        a. If not specify the oldSuffix, any suffix will be changed to newSuffix
        b. if specify the oldSuffix, only specified suffix be changed to newSuffix
        
        Note:
        when specify the suffix, you need to add extension separator, eg, specify '.txt', not 'txt'.
    '''    
    def __init__(self):
        self.wholelist=[]

    def menulist(self,path, suffix=None):
        '''
        Add the files under the path to a list.
        a. if the suffix is specified, only files with this suffix will be added to the list.
        b. if the suffix is not specified, all files under this suffix will be added to the list.
        '''
        path = os.path.expanduser(path)
        if not os.path.exists(path):
            print "The path %s doesn't exist" % path
            exit()
        if suffix == None:
            if os.path.isdir(path):
                lista = os.listdir(path)
                for submenu in lista:
                    submenu = os.path.join(path,submenu)
                    if os.path.isdir(submenu):
                        self.menulist(submenu)
                    else:
                        self.wholelist.append(submenu)                    
            else:
                self.wholelist.append(path)
        else:
            if os.path.isdir(path):
                lista = os.listdir(path)
                for submenu in lista:
                    submenu = os.path.join(path,submenu)
                    if os.path.isdir(submenu):
                        self.menulist(submenu,suffix)
                    else:
                        if os.path.splitext(submenu)[1] == suffix:
                            self.wholelist.append(submenu) 
            else:
                if os.path.splitext(path)[1] == suffix:
                    self.wholelist.append(path)
                        
    def changeExt(self,path,newSuffix,oldSuffix=None):
        '''
        Change the suffix of the files of a specific path
        a. If not specify the oldSuffix, any suffix will be changed to newSuffix
        b. if specify the oldSuffix, only specified suffix be changed to newSuffix
        
        Note:
        when specify the suffix, you need to add extension separator, eg, specify '.txt', not 'txt'.
        '''
        if oldSuffix == None:
            self.menulist(path)
        else:
            self.menulist(path,oldSuffix)
        print "list of files need to change extension name: %s" % (self.wholelist) 
        for item in self.wholelist:
            print item
            os.rename(item, os.path.splitext(item)[0]+newSuffix)
            print "rename '%s' to '%s'" % (item, os.path.splitext(item)[0]+newSuffix)
    
if __name__ == "__main__":
    rename = ChangeExt()
    rename.changeExt("c:\\vms", '.log')            # not specify the oldSuffix
    rename.changeExt("c:\\vms", '.txt','.log')       # specify the oldSuffix


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐