Java项目替换包名
import osdef process_directory(path):for root, dirs, files in os.walk(path, topdown=False):# 处理文件for name in files:file_path = os.path.join(root, name)new_name = name.replace('xxxx', 'ruphy').replace('XxXx', 'Ruphy').replace('Xxxx', 'Ruphy')new_file_path = os.path.join(root, new_name)if name != new_name:# 检查新文件名是否已存在if not os.path.exists(new_file_path):os.rename(file_path, new_file_path)# 如果是文本文件,替换内容if name != 'node_modules' and new_name.endswith(('.txt', '.properties', '.java', '.xml', '.vue', '.js', '.md', '.yml', '.css', '.wxss', '.json', '.wxml', '.html', '.htm', '.vm', '.sql', 'Dockerfile')):try:with open(new_file_path, 'r', encoding='utf-8') as file:content = file.read()with open(new_file_path, 'w', encoding='utf-8') as file:file.write(content.replace('xxxx', 'ruphy').replace('://ruphy', '://xxxx').replace('XxXx', 'Ruphy').replace('Xxxx', 'Ruphy'))except UnicodeDecodeError:# 如果文件不是 UTF-8 编码,可以尝试其他编码或忽略错误print(f"Warning: Could not decode file {new_file_path} as UTF-8. Skipping.")# 处理目录for name in dirs:dir_path = os.path.join(root, name)new_name = name.replace('xxxx', 'ruphy').replace('XxXx', 'Ruphy').replace('Xxxx', 'Ruphy')new_dir_path = os.path.join(root, new_name)if name != new_name:# 检查新目录名是否已存在if not os.path.exists(new_dir_path):os.rename(dir_path, new_dir_path)# 不处理空目录的删除,因为存在冲突# 检查是否为空目录且名称为xxxx,如果是则删除elif name == 'xxxx' and not os.listdir(dir_path):os.rmdir(dir_path)elif name == 'XxXx' and not os.listdir(dir_path):os.rmdir(dir_path)# 指定要处理的目录路径
directory_to_process = 'D:\\workspace\\ysgz\\java'
process_directory(directory_to_process)