如何使用 python 从文件中剪切一些行并将它们粘贴到同一文件中的特定位置?

How to cut some lines from a file and paste them at certain position in a same file using python?

我有一个名为 'test.txt' 的文件,其中包含如下文本:

<<Title here>>

Hello there

Please cut me and paste somewhere else

Bye there

我要剪线-

Please cut me and paste somewhere else

并且,将它们粘贴在此处标题之后的顶部,如下所示:-

<<Title here>>

Please cut me and paste somewhere else

Hello there

Bye there

我想使用 python 在同一文件中执行 cut-paste 操作。我怎样才能达到同样的效果?

我认为你想多了。不是什么难的问题。

parts = open('x.txt').read().split('\n\n')
parts.insert( 1, parts.pop(2))
print('\n\n'.join(parts))