在MTK feature phone平台,MTK会把一些.bmp文件写成.pbm的后缀,对于开发者来说,因为.pbm的文件不能直接看效果,所以一般会手动重命名之后看效果。这样的更改方法,一个两个图片还可以,如果要批量查看,则会影响工作效率。
这里介绍一种批量修改文件名后缀的方法。下面代码可以通过使用python中的os.walk方法实现一个目录中文件的遍历,并结合一些os函数实现批量文件的重命名。
# -*- coding: utf-8 -*- # It is ok at python-3.3.1rc1.msi installer condition. import os import re # change the following dir to your dir walk_names = os.walk(r ' F:\view\mtk_6250\plutommi\Customer\Images\FTE_SLIM320X480\MainLCD ') for (directory, sub_directorys, file_names) in walk_names: for name in file_names: m = re.match(r ' (.+\.)pbm$ ', name, re.I) if m: src = os.path.join(directory, name) dst = os.path.join(directory, m.group(1)) + ' bmp ' if not os.path.isfile(dst): os.rename(src, dst) print(dst) else: print( " This file exist: ") print(dst + ' \n ')