本文实例讲述了Python实现批量更换指定目录下文件扩展名的方法。分享给大家供大家参考,具体如下: #encoding=utf-8#author: walker#date: 2013-12-06#function: 深度遍历指定目录,更换指定扩展名import osimport os.path#读入指定目录并转换为绝对路径rootdir = raw_input("root dir: ")rootdir = os.path.abspath(rootdir)print("absolute path: " + rootdir)#读入原扩展名并标准化old_ext = raw_input("old extension: ")old_ext = old_ext.strip()if old_ext[0] != ".": old_ext = "." + old_ext#读入新扩展名并标准化new_ext = raw_input("new extension: ")new_ext = new_ext.strip()if new_ext[0] != ".": new_ext = "." + new_extfor parent, dirnames, filenames in os.walk(rootdir): for filename in filenames: pathfile = os.path.join(parent, filename) if pathfile.endswith(old_ext): new_pathfile = os.path.splitext(pathfile)[0] + new_ext print("=======================================================") print(pathfile) print("-------------……
python文件的扩展名-Python怎样获取文件扩展名
最新推荐文章于 2024-03-09 07:26:01 发布