# -*- coding:utf8 -*- import os import random def rename(path): oldpath = path pos1 = path.find('.luac') # pos2 = path.find('_', pos1+5) newpath = path[:pos1]+'.lua' os.rename(path, newpath) # 对文件进行重命名 def test(path): files = os.listdir(path) # 获取当前目录的所有文件及文件夹 for file in files: try: file_path = os.path.join(path, file) # 获取绝对路径 if os.path.isdir(file_path): # 判断是否是文件夹 test(file_path) # 如果是文件夹,就递归调用自己 else: print(file_path) extension_name = os.path.splitext( file_path) # 将文件的绝对路径中的后缀名分离出来 if extension_name[1] == '.luac': rename(file_path) except: continue # 可能会报错,所以用了try-except,如果要求比较严格,不需要报错,就删除异常处理,自己调试 test(r'/Users/jichonggula/Desktop/src') # for old_name in os.listdir(address): # portion = os.path.splitext(old_name) # if portion[1] == ".luac": # 旧的后缀 # new_name = portion[0] + ".lua" # 新的后缀 # old_name = os.path.join(address, old_name) # # os.rename的参数需要的是路径和文件名,所以这里要加上路径,要不然脚本执行失败。 # new_name = os.path.join(address, new_name) # print(old_name) # print(new_name) # os.rename(old_name, new_name)