直接上代码:
import os
import sys
#递归遍历文件夹名,筛选符合要求的文件夹,并执行替换重命名
def replaceDirName(rootDir, oldStr, newStr):
for dir in os.listdir(rootDir):
dirpath=rootDir+"/"+dir
print("dirpath = ", dirpath)
if os.path.isdir(dirpath):
isIn=oldStr in dir
if isIn==False:
replaceDirName(dirpath,oldStr,newStr)
continue
dir=dir.replace(oldStr, newStr)
print("dir = ", dir)
newDirPath=rootDir+"/"+dir
os.rename(dirpath,newDirPath)
replaceDirName(newDirPath,oldStr,newStr)
def main(argv):
print("argv = ", argv[0])
rootDir = argv[1]
oldStr = argv[2]
newStr = argv[3]
replaceDirName(rootDir, oldStr, newStr)
#执行流
if __name__ == '__main__':
main(sys.argv)