Python - 将行从一个文件移动到另一个文件

时间:2017-09-07 15:26:49

标签: python python-3.x

我正在尝试读取文本文件,将每行复制到新文件,并从原始文件中删除该行。不知道如何删除该行。任何帮助是极大的赞赏。到目前为止,这是我的代码:

    with open("TestList_in.txt",'r+') as f:
        for lineNum in f:
            lineNum.replace("\n","")
            with open("TestList_done.txt","a") as done:
                done.write(lineNum)

1 个答案:

答案 0 :(得分:0)

为了创建所有工作在同一文本文件中的线程,删除读取的每一行都没有必要。相反,导入队列模块并将行放入队列中。

from queue import Queue

q = Queue()

with open("TestList_in.csv",'r+') as f:
    for lineNum in f:
        q.put(lineNum)
        with open("TestList_done.txt","a") as done:
            newLine = q.get()
            done.write(newLine)