Python 文件重命名的小工具

# -*- coding: utf-8 -*-
#python3.3
import os
import traceback
def RenameFile(rootDir,LstoRemove):
    for lists in os.listdir(rootDir):
        path = os.path.join(rootDir, lists)
        #print (path)
        for strtoRemove in LstoRemove:
            index = path.find(strtoRemove)
            if index >= 0:
                newPath = path[index + len(strtoRemove):]
                print(newPath)
                newPath = os.path.join(rootDir, newPath)
                try:
                    os.rename(path,newPath)
                except:
                    traceback.print_exc()
        if os.path.isdir(path):
            RenameFile(path, LstoRemove)
    print (rootDir + "  work done!")
dir = r"G:\xunlei"
LstoRemove = [r"paiohua.com",r"piaohua.com"]#可以扩展自己要去掉的字符
RenameFile(dir, LstoRemove)



你可能感兴趣的:(Python 文件重命名的小工具)