我需要从一个文本文档中剪切特定数量的行并将其粘贴到另一个文本文档中

时间:2019-08-30 07:15:41

标签: python

我需要将一个文本文档中的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.

enter image description here

1 个答案:

答案 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