Python 批量转换文件扩展名

来源:互联网 发布:生成式对抗网络 知乎 编辑:程序博客网 时间:2024/05/18 03:54

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

#!/usr/bin/python#-*- encoding:utf-8 -*-import osclass 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


原创粉丝点击