时间:2021-07-01 10:21:17 帮助过:38人阅读
代码如下:
# -*- 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()