以下是一个示例解决方案,可以按照光标位置将文件拆分为两个部分:
def split_file_by_cursor_position(filename, cursor_position):
with open(filename, 'r') as file:
content = file.read()
# 根据光标位置将内容拆分为两部分
part1 = content[:cursor_position]
part2 = content[cursor_position:]
# 将拆分后的内容写入两个新文件
with open('part1.txt', 'w') as file:
file.write(part1)
with open('part2.txt', 'w') as file:
file.write(part2)
# 测试
filename = 'example.txt' # 假设有一个名为 example.txt 的文件
cursor_position = 10 # 假设光标位置在第 10 个字符后面
split_file_by_cursor_position(filename, cursor_position)
以上代码将会按照光标位置拆分文件,将光标位置之前的内容保存为 part1.txt
,将光标位置之后的内容保存为 part2.txt
。你可以根据实际需求修改文件名和光标位置的值。