python批量修改文件后缀示例代码分享
代码如下:
# -*- coding:utf-8 -*-
import os
def rename():
path = raw_input("请输入要处理的文件夹路径")
print path
old_ext = "."+raw_input("请输入要处理的文件类型")
print old_ext
new_ext = "."+raw_input("请输入你想变成的文件类型")
print new_ext
for (path, dirs, files) in os.walk(path):#遍历目录树
for filename in files:
ext = os.path.splitext(filename)[1] #取得文件类型,注意它还带着点号
print ext;
if(ext == old_ext):
print "----------------"
newname = filename.replace(old_ext, new_ext)
oldpath = path + "\\" + filename
newpath = path + "\\" + newname
try:
os.rename(oldpath, newpath)
except BaseException, e:
print(str(e))
if __name__ == '__main__':
rename()
上一篇: PHP实现冒泡排序几个实例
下一篇: php 远程下载图片到本地,_PHP教程