[英]Python - Move line from one file to another
我正在尝试读取文本文件,将每行复制到新文件,并从原始文件中删除该行。 不知道如何删除该行。 任何帮助是极大的赞赏。 到目前为止,这是我的代码:
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)
为了创建所有工作在同一文本文件中的线程,没有必要在读取时删除每一行。 相反,导入队列模块并将行放入队列中。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.