python文件的扩展名-Python怎样获取文件扩展名

本文实例讲述了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("-------------……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值