| | 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) |