|
@@ -35,3 +35,24 @@ def append_block_in_file(file_path, new_block):
|
|
file.write('\n' + new_block)
|
|
file.write('\n' + new_block)
|
|
except Exception as e:
|
|
except Exception as e:
|
|
raise BusinessException(message=f"追加文件内容失败, {e}", code=-1)
|
|
raise BusinessException(message=f"追加文件内容失败, {e}", code=-1)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def insert_code_after_line(file_path, line_number, code_block):
|
|
|
|
+ # 读取文件内容
|
|
|
|
+ with open(file_path, 'r') as file:
|
|
|
|
+ lines = file.readlines()
|
|
|
|
+
|
|
|
|
+ # 确保 line_number 合法
|
|
|
|
+ if line_number < 1 or line_number > len(lines):
|
|
|
|
+ print(f"Line number {line_number} is out of range.")
|
|
|
|
+ return
|
|
|
|
+
|
|
|
|
+ # 将代码块拆分成多行
|
|
|
|
+ code_lines = code_block.splitlines(True)
|
|
|
|
+
|
|
|
|
+ # 在指定行后插入代码块
|
|
|
|
+ lines[line_number:line_number] = code_lines
|
|
|
|
+
|
|
|
|
+ # 写回文件
|
|
|
|
+ with open(file_path, 'w') as file:
|
|
|
|
+ file.writelines(lines)
|