#手动选择路径,批量改名
import os,re,time,tkFileDialog
global i
#文件名后面增加后缀:txt,png,bng,jpeg,jpg,gif,zip类型的文件
def change_name(path,text):
global i
if not os.path.isdir(path) and not os.path.isfile(path):
return False
elif os.path.isfile(path):
file_text=os.path.split(path)
lists=file_text[-1].split('.')
os.rename(path,file_text[0]+'\\'+lists[0]+text+'.'+lists[-1])#添加后缀
i+=1
elif os.path.isdir(path):
for x in os.listdir(path):
change_name(os.path.join(path,x), text)
#修改后缀名,将图片统一修改为jpg
def change_ext(path):
global i
if not os.path.isdir(path) and not os.path.isfile(path):
return False
elif os.path.isfile(path):
file_text=os.path.split(path)
lists=file_text[-1].split('.')
mange=['jpeg','png','gif','bng','psd']
if lists[1] in mange:
os.rename(path,file_text[0]+'\\'+lists[0]+'.'+'jpg')
i+=1
elif os.path.isdir(path):
for x in os.listdir(path):
change_ext(os.path.join(path,x))
print'0代表更换文件名,其它代表改扩展名'
name=int(raw_input('请选择更换文件名还是改变扩展名:'))
lj=tkFileDialog.askdirectory()
i=0
if name==0:
gb_name=str(raw_input('输入添加的后缀:'))
start_time=time.time()
change_name(lj,gb_name)
end_time=time.time()
print '开始时间:',start_time,'结束时间:',end_time,'用时:',end_time-start_time
print('共处理了%s个文件'%(i))
else:
start_time=time.time()
change_ext(lj)
end_time=time.time()
print '开始时间:',start_time,'结束时间:',end_time,'用时:',end_time-start_time
print('共处理了%s个图片'%(i))