在 python 中将行从一个文件复制到另一个文件

copying lines from one file to another in python

好的,所以我的 class 作业是编写代码,将每一行从一个文本文件复制到一个新的文本文件。我觉得我的头撞墙太多了,再也看不到我在看什么了。

这是我的:

source_file = open('data1.txt', 'r')
line = numbers_file.readline()
destination_file = open('data2.txt', 'w')
source_file.seek(0)
for line in source_file:
    destination_file.writelines(line)
source_file.close()
destination_file.close()
# opens original file
file1 = open("data1.txt" , "r")
# opens new file
file2 = open("data2.txt" , "w")
#for each line in old file
for line in file1:
#write that line to the new file
    file2.write(line)
#close file 1
file1.close()
#close file2
file2.clsoe()