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
- Python 批量转换文件扩展名
- 批量转换文件扩展名
- python实现批量修改文件扩展名
- 批量修改文件扩展名
- 批量修改文件扩展名
- linux下批量转换扩展名
- python批量转换文件编码
- python批量转换文件编码
- 文件扩展名批量修改器
- 批量更改文件的扩展名
- 怎么批量修改文件扩展名
- windows批量添加文件扩展名
- python 文件扩展名
- dos2unix 批量将dos格式的文件转换成unix格式,支持文件扩展名自定义
- 批量文件编码转换-python
- Python 批量转换文件编码格式
- Python小脚本—批量修改文件名与文件扩展名升级版
- Python小脚本——批量修改文件名与文件扩展名
- 家庭用药助手:丁香园推出面向大众家庭用户的药品信息服务工具
- hive学习笔记1
- RSA - Java编程: 用Cipher进行RSA加解密
- linux下查看线程数的几种方法
- RSA - Java编程:生成RSA密钥对
- Python 批量转换文件扩展名
- URL的井号
- js图片无缝滚动代码
- rp_filter及Linux下多网卡接收多播的问题
- 如何应对海量数据时代的挑战
- 怎么设置机器远程访问Personal ArcSDE
- Eclipse构建下构建ant<二>
- 笑话收集
- 翻转字符串Java实现