Python 批量转换文件扩展名

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

#!/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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值