Upload transfer.py with huggingface_hub
Browse files- transfer.py +38 -0
transfer.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
def replace_string_in_file(file_path, old_str, new_str):
|
| 4 |
+
"""替换文件中的字符串"""
|
| 5 |
+
try:
|
| 6 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 7 |
+
content = file.read()
|
| 8 |
+
|
| 9 |
+
# 替换字符串
|
| 10 |
+
new_content = content.replace(old_str, new_str)
|
| 11 |
+
|
| 12 |
+
# 如果内容有变化,则写回文件
|
| 13 |
+
if new_content != content:
|
| 14 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
| 15 |
+
file.write(new_content)
|
| 16 |
+
print(f"已替换文件: {file_path}")
|
| 17 |
+
else:
|
| 18 |
+
print(f"无需替换: {file_path}")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"处理文件 {file_path} 时出错: {e}")
|
| 21 |
+
|
| 22 |
+
def traverse_directory(directory, old_str, new_str):
|
| 23 |
+
"""遍历目录下的所有文件"""
|
| 24 |
+
for root, dirs, files in os.walk(directory):
|
| 25 |
+
for file_name in files:
|
| 26 |
+
if file_name == 'transfer.py':
|
| 27 |
+
continue
|
| 28 |
+
file_path = os.path.join(root, file_name)
|
| 29 |
+
replace_string_in_file(file_path, old_str, new_str)
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
# 设置目录路径和要替换的字符串
|
| 33 |
+
directory = "/fs-computility/niuyazhe/lixueyan/meme/InternVL" # 替换为你的目录路径
|
| 34 |
+
old_str = "ai-shen" # 要替换的字符串
|
| 35 |
+
new_str = "niuyazhe" # 替换后的字符串
|
| 36 |
+
|
| 37 |
+
# 遍历目录并替换字符串
|
| 38 |
+
traverse_directory(directory, old_str, new_str)
|