我需要将一个文本文档中的n行剪切并粘贴到另一行中,我不知道该用什么来删除刚刚复制的行。
x = 1
while x == 1:
copy = 2
count = 0
print ('test1=')
x = 2
with open("try3.txt") as f:
with open("try4.txt", "a") as f1:
for line in f:
if count < 63:
count = count +1
print ('test2=')
f1.write(line)
f.
答案 0 :(得分:1)
a.txt
a
b
c
d
b.txt
代码:
a=open('a.txt','r').readlines() #['a\n', 'b\n', 'c\n', 'd']
b=a[0:2] #['a\n', 'b\n']
open('b.txt','w').writelines(b) # write b to b.txt
for b_element in b:
a.remove(b_element) # remove b element in 'a' list
open('a.txt','w').writelines(a) # write a to a.txt
结果:
a.txt
c
d
b.txt
a
b