Python批量重命名文件

批量替换文件名中重复字符:

# -*- coding: UTF-8 -*-
import os

path = raw_input("请输入文件夹路径:")
oldname = raw_input("输入被替换的字符:")
newname = raw_input("输入新的字符(如为空直接回车):")

for file in os.listdir(path):
    if os.path.isfile(os.path.join(path,file))==True:
         if file.find(oldname) > -1:
             new_name=file.replace(oldname,newname)
             os.rename(os.path.join(path,file),os.path.join(path,new_name))
             
             print 'OK! ',new_name

 

 按文件顺序重新以序号命名:

# -*- coding: UTF-8 -*-
import os
 
path = raw_input("请输入文件夹路径:")
i = int(raw_input("开始序号:"))

for file in os.listdir(path):
    if os.path.isfile(os.path.join(path,file))==True:
        new_name = file.replace(file,"%d.jpg"%i)
        os.rename(os.path.join(path,file),os.path.join(path,new_name))
        i+=1
        print 'OK! ',new_name

 

posted on 2013-12-24 21:37  chinahuzi  阅读(659)  评论(0编辑  收藏  举报