import os def replace_string_in_file(file_path, old_str, new_str): """替换文件中的字符串""" try: with open(file_path, 'r', encoding='utf-8') as file: content = file.read() # 替换字符串 new_content = content.replace(old_str, new_str) # 如果内容有变化,则写回文件 if new_content != content: with open(file_path, 'w', encoding='utf-8') as file: file.write(new_content) print(f"已替换文件: {file_path}") else: print(f"无需替换: {file_path}") except Exception as e: print(f"处理文件 {file_path} 时出错: {e}") def traverse_directory(directory, old_str, new_str): """遍历目录下的所有文件""" for root, dirs, files in os.walk(directory): for file_name in files: if file_name == 'transfer.py': continue file_path = os.path.join(root, file_name) replace_string_in_file(file_path, old_str, new_str) if __name__ == "__main__": # 设置目录路径和要替换的字符串 directory = "/fs-computility/niuyazhe/lixueyan/meme/InternVL" # 替换为你的目录路径 old_str = "ai-shen" # 要替换的字符串 new_str = "niuyazhe" # 替换后的字符串 # 遍历目录并替换字符串 traverse_directory(directory, old_str, new_str)